Cara menggunakan replace unicode characters php

Everyone working with JavaScript will have to deal with strings at one point or other. Sometimes, you will just have to store a string inside another variable and then pass it over. Other times, you will have to inspect it and see if it contains a particular substring.

However, things are not always this easy. There will be times when you will not be looking for a particular substring but a set of substrings which follow a certain pattern.

Let's say you have to replace all occurrences of "Apples" in a string with "apples". You could simply use

"I ate Apples".replace("Apples", "apples");
26. Nice and easy.

Now let's say you have to replace "appLes" with "apples" as well. Similarly, "appLES" should become "apples" too. Basically, all case variations of "Apple" need to be changed to "apple". Passing simple strings as an argument will no longer be practical or efficient in such cases.

This is where regular expressions come in—you could simply use the case-insensitive flag

"I ate Apples".replace("Apples", "apples");
27 and be done with it. With the flag in place, it doesn't matter if the original string contained "Apples", "APPles", "ApPlEs", or "Apples". Every instance of the word will be replaced with "apples".

Just like the case-insensitive flag, regular expressions offer a lot of other features which will be covered in this tutorial.

Using Regular Expressions in JavaScript

You have to use a slightly different syntax to indicate a regular expression inside different

"I ate Apples".replace("Apples", "apples");
28 methods. Unlike a simple string, which is enclosed in quotes, a regular expression consists of a pattern enclosed between slashes. Any flags that you use in a regular expression will be appended after the second slash.

Going back to the previous example, here is what the

"I ate Apples".replace("Apples", "apples");
29 method would look like with a regular expression and a simple string.

1
"I ate Apples".replace("Apples", "apples");
2
// I ate apples

3
4
"I ate Apples".replace(/Apples/i, "apples");
5
// I ate apples

6
"I ate Apples".replace("Apples", "apples");
0
"I ate Apples".replace("Apples", "apples");
1
"I ate Apples".replace("Apples", "apples");
2
"I ate Apples".replace("Apples", "apples");
3
"I ate Apples".replace("Apples", "apples");
4
"I ate Apples".replace("Apples", "apples");
5
"I ate Apples".replace("Apples", "apples");
6
"I ate Apples".replace("Apples", "apples");
7
"I ate Apples".replace("Apples", "apples");
8

As you can see, the regular expression worked in both cases. We will now learn more about flags and special characters that make up the pattern inside a regular expression.

Backslash in Regular Expressions

You can turn normal characters into special characters by adding a backslash before them. Similarly, you can turn special characters into normal characters by adding a backslash before them.

For example,

"I ate Apples".replace("Apples", "apples");
30 is not a special character. However,
"I ate Apples".replace("Apples", "apples");
31 is used to match a digit character in a string. Similarly,
"I ate Apples".replace("Apples", "apples");
32 is not a special character either, but
"I ate Apples".replace("Apples", "apples");
33 is used to match non-digit characters in a string.

Digit characters include 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9. When you use

"I ate Apples".replace("Apples", "apples");
31 inside a regular expression, it will match any of these nine characters. When you use
"I ate Apples".replace("Apples", "apples");
33 inside a regular expression, it will match all the non-digit characters.

The following example should make things clear.

1
2
0
2
2
2
3
4
2
5
5
2
7
6
"I ate Apples".replace("Apples", "apples");
0
// I ate apples

0
"I ate Apples".replace("Apples", "apples");
2
// I ate apples

2

You should note that only the first matched character is replaced in the third case. You can also use flags to replace all the matches. We will learn about such flags later.

Just like

"I ate Apples".replace("Apples", "apples");
31 and
"I ate Apples".replace("Apples", "apples");
33, there are other special character sequences as well.

  1. You can use
    "I ate Apples".replace("Apples", "apples");
    
    38 to match any "word" character in a string. Here, word character refers to A-Z, a-z, 0-9, and _. So, basically, it will match all digits, all lowercase and uppercase alphabets, and the underscore.
  2. You can use
    "I ate Apples".replace("Apples", "apples");
    
    39 to match any non-word character in a string. It will match characters like %, $, #, ₹, etc.
  3. You can use
    "I ate Apples".replace("Apples", "apples");
    
    40 to match a single white space character, which includes space, tab, form feed, and line feed. Similarly, you can use
    "I ate Apples".replace("Apples", "apples");
    
    41 to match all other characters besides white space.
  4. You can also look for a specific white space character using
    "I ate Apples".replace("Apples", "apples");
    
    42,
    "I ate Apples".replace("Apples", "apples");
    
    43,
    "I ate Apples".replace("Apples", "apples");
    
    44,
    "I ate Apples".replace("Apples", "apples");
    
    45, and
    "I ate Apples".replace("Apples", "apples");
    
    46, which stand for form feed, line feed, carriage return, horizontal tab, and vertical tab.

Sometimes, you will face situations where you need to replace a word with its substitute, but only if it is not part of a larger word. For example, consider the following sentence:

"A lot of pineapple images were posted on the app".

In this case, we want to replace the word "app" with "board". However, using a simple regular expression pattern will turn "apple" into "boardle", and the final sentence would become:

"A lot of pineboardle images were posted on the app".

In such cases, you can use another special character sequence: 

"I ate Apples".replace("Apples", "apples");
47. This checks for word boundaries. A word boundary is formed by use of any non-word characters like space, "$", "%", "#", etc. Watch out, though—it also includes accented characters like "ü".

1
// I ate apples

4
2
// I ate apples

6
3
4
// I ate apples

9
5
3
1

Similarly, you can use

"I ate Apples".replace("Apples", "apples");
48 to match a non-word boundary. For example, you could use
"I ate Apples".replace("Apples", "apples");
48 to only match "app" when it is within another word, like "pineapple".

Matching a Pattern "n" Number of Times

You can use

"I ate Apples".replace("Apples", "apples");
50 to tell JavaScript to only look at the beginning of the string for a match. Similarly, you can use
"I ate Apples".replace("Apples", "apples");
51 to only look at the end of the string for a match.

You can use

"I ate Apples".replace("Apples", "apples");
52 to match the preceding expression 0 or more times. For example,
"I ate Apples".replace("Apples", "apples");
53 will match A, Ap, App, Appp, and so on.

In a similar manner, you can use

"I ate Apples".replace("Apples", "apples");
54 to match the preceding expression 1 or more times. For example,
"I ate Apples".replace("Apples", "apples");
55 will match Ap, App, Appp, and so on. The expression will not match the single A this time.

Sometimes, you only want to match a specific number of occurrences of a given pattern. In such cases, you should use the

"I ate Apples".replace("Apples", "apples");
56 character sequence, where n is a number. For instance,
"I ate Apples".replace("Apples", "apples");
57 will match App but not Ap. It will also match the first two 'p's in Appp and leave the third one untouched.

You can use

"I ate Apples".replace("Apples", "apples");
58 to match at least 'n' occurrences of a given expression. This means that
"I ate Apples".replace("Apples", "apples");
59 will match App but not Ap. It will also match all the 'p's in Apppp and replace them with your replacement string.

You can also use

"I ate Apples".replace("Apples", "apples");
60 to specify a minimum and maximum number and limit the number of times the given expression should be matched. For example,
"I ate Apples".replace("Apples", "apples");
61 will match App, Appp, and Apppp. It will also match the first four 'p's in Apppppp and leave the rest of them untouched.

1
3
3
2
3
5
3
4
3
8
5
3
5
6
"I ate Apples".replace("Apples", "apples");
0
4
3
"I ate Apples".replace("Apples", "apples");
2
4
5
"I ate Apples".replace("Apples", "apples");
4
"I ate Apples".replace("Apples", "apples");
5
4
8
"I ate Apples".replace("Apples", "apples");
7
"I ate Apples".replace(/Apples/i, "apples");
0
"I ate Apples".replace(/Apples/i, "apples");
1
"I ate Apples".replace(/Apples/i, "apples");
2
"I ate Apples".replace(/Apples/i, "apples");
3
"I ate Apples".replace(/Apples/i, "apples");
4
"I ate Apples".replace(/Apples/i, "apples");
5

Using Parentheses to Remember Matches

So far, we have only replaced patterns with a constant string. For example, in the previous section, the replacement we used was always "Add". Sometimes, you will have to look for a pattern match inside the given string and then replace it with a part of the pattern.

Let's say you have to find a word with five or more letters in a string and then add an "s" at the end of the word. In such cases, you will not be able to use a constant string value as a replacement as the final value depends on the matching pattern itself.

1
"I ate Apples".replace(/Apples/i, "apples");
7
2
"I ate Apples".replace(/Apples/i, "apples");
9
3
4
5
2
5
5
4

This was a simple example, but you can use the same technique to keep more than one matching pattern in memory. The number of sub-patterns in the full match will be determined by the number of parentheses used.

Inside the replacement string, the first sub-match will be identified using

"I ate Apples".replace("Apples", "apples");
62, the second sub-match will be identified using
"I ate Apples".replace("Apples", "apples");
63, and so on. Here is another example to further clarify the usage of parentheses.

1
5
6
2
5
8

Using Flags With Regular Expressions

As I mentioned in the introduction, one more important feature of regular expressions is the use of special flags to modify how a search is performed. The flags are optional, but you can use them to do things like making a search global or case-insensitive.

These are the four commonly used flags to change how JavaScript searches or replaces a string.

  • "I ate Apples".replace("Apples", "apples");
    
    64: This flag will perform a global search instead of stopping after the first match.
  • "I ate Apples".replace("Apples", "apples");
    
    27: This flag will perform a search without checking for an exact case match. For instance, Apple, aPPLe, and apPLE are all treated the same during case-insensitive searches.
  • "I ate Apples".replace("Apples", "apples");
    
    66: This flag will perform a multi-line search.
  • "I ate Apples".replace("Apples", "apples");
    
    67: This flag will look for a match in the index indicated by the
    "I ate Apples".replace("Apples", "apples");
    
    68 property.

Here are some examples of regular expressions used with flags:

1
// I ate apples

0
2
// I ate apples

2
3
4
// I ate apples

5
5
// I ate apples

7
6
"I ate Apples".replace("Apples", "apples");
0
6
0
"I ate Apples".replace("Apples", "apples");
2
6
2
"I ate Apples".replace("Apples", "apples");
4
"I ate Apples".replace("Apples", "apples");
5
6
5
"I ate Apples".replace("Apples", "apples");
7
// I ate apples

7
"I ate Apples".replace(/Apples/i, "apples");
1
"I ate Apples".replace(/Apples/i, "apples");
2
"I ate Apples".replace(/Apples/i, "apples");
4
"I ate Apples".replace("Apples", "apples");
01
"I ate Apples".replace("Apples", "apples");
02
"I ate Apples".replace("Apples", "apples");
03
"I ate Apples".replace("Apples", "apples");
04
"I ate Apples".replace("Apples", "apples");
05
"I ate Apples".replace("Apples", "apples");
06
"I ate Apples".replace("Apples", "apples");
07
"I ate Apples".replace("Apples", "apples");
08
"I ate Apples".replace("Apples", "apples");
09
"I ate Apples".replace("Apples", "apples");
01
"I ate Apples".replace("Apples", "apples");
11
"I ate Apples".replace("Apples", "apples");
12
"I ate Apples".replace("Apples", "apples");
13
"I ate Apples".replace("Apples", "apples");
05
"I ate Apples".replace("Apples", "apples");
15
// I ate apples

2
"I ate Apples".replace("Apples", "apples");
17
"I ate Apples".replace("Apples", "apples");
18
"I ate Apples".replace("Apples", "apples");
01
"I ate Apples".replace("Apples", "apples");
20
"I ate Apples".replace("Apples", "apples");
21
"I ate Apples".replace("Apples", "apples");
22
"I ate Apples".replace("Apples", "apples");
05
"I ate Apples".replace("Apples", "apples");
24
"I ate Apples".replace("Apples", "apples");
25

Final Thoughts

The purpose of this tutorial was to introduce you to regular expressions in JavaScript and their importance. We began with the basics and then covered backslash and other special characters. We also learned how to check for a repeating pattern in a string and how to remember partial matches in a pattern in order to use them later.

Finally, we learned about commonly used flags which make regular expressions even more powerful. You can learn more about regular expressions in this article on MDN.