JavaScript regular expression analysis page 1 / 2

Time:2021-9-19

What is a regular expression?
For this problem, see “regular expression 30 minute tutorial”. Many programming languages support regular expressions. This article only discusses regular expressions in JavaScript.
Create a regular expression
The first method:

Copy codeThe code is as follows:
var reg = /pattern/;

The second method:

Copy codeThe code is as follows:
var reg = new RegExp(‘pattern’);

Introduction to exec method of regular expression
Syntax:
reg.exec(str);
Where STR is the target string to execute the regular expression.
For example:

Copy codeThe code is as follows:
<script type=”text/javascript“>
var reg = /test/;
var str = ‘testString’;
var result = reg.exec(str);
alert(result);
</script>

Test will be output because the regular expression reg matches the ‘test’ substring in str (‘teststring ‘) and returns it.

Let’s use the following function to do the regular matching exercise:

Copy codeThe code is as follows:
function execReg(reg,str){
var result = reg.exec(str);
alert(result);
}

The function accepts a regular expression parameter REG and a target string parameter str. after execution, it will alert the matching result between the regular expression and the string.
Use this function to test the above example:

Copy codeThe code is as follows:
<script type=”text/javascript”>
function execReg(reg,str){
var result = reg.exec(str);
alert(result);
}
var reg = /test/;
var str = ‘testString’;
execReg(reg,str);
</script>

The above example uses the test in the regular to match the test in the string. It is really boring. The same task can be completed with the indexof method. With regular, it is natural to complete more powerful functions:
One, two, three or four, all gone
The subtitle above translates into {1}, {2}, {3,4}, {1,}.
c{n}
{1} Means a.
/C {1} / can only match one C.
/C {2} / will match two consecutive C.
and so on,
/C {n} / will match n consecutive C.
Take the following example:

Copy codeThe code is as follows:
reg = /c{1}/;
str=’cainiao’;
execReg(reg,str);

Return result c

Copy codeThe code is as follows:
reg = /c{2}/;
str=’cainiao’;
execReg(reg,str);

The returned result is null, indicating that no matching succeeded.

Copy codeThe code is as follows:
reg = /c{2}/;
STR =’ccvc jelly cool ‘;
execReg(reg,str);

Returns the result CC.
c{m,n}
C {3,4} means three or four consecutive C’s.
for example

Copy codeThe code is as follows:
reg = /c{3,4}/;
STR =’ccvc jelly cool ‘;
execReg(reg,str);

The returned result is null, indicating that no matching succeeded.

Copy codeThe code is as follows:
reg = /c{3,4}/;
str=’cccTest’;
execReg(reg,str);

The result returns CCC.

Copy codeThe code is as follows:
reg = /c{3,4}/;
str=’ccccTest’;
execReg(reg,str);

The result returns CCCC, which indicates that regular will try to match as many brands as possible. When it can be 3 or 4, it will choose to match more than one.

Copy codeThe code is as follows:
reg = /c{3,4}/;
str=’cccccTest’;
execReg(reg,str);

Still only four C’s match.

It can be inferred from the above example that C {m, n} represents m to N C, and M is less than or equal to n.
c{n,}
C {1,} represents more than one C. For example:

Copy codeThe code is as follows:
reg = /c{1,}/;
str=’cainiao’;
execReg(reg,str);

The result returns C.

Copy codeThe code is as follows:
reg = /c{1,}/;
str=’cccccTest’;
execReg(reg,str);

Ccccc is returned, which again shows that regular expressions will match as many as possible.

Copy codeThe code is as follows:
reg = /c{2,}/;
str=’cainiao’;
execReg(reg,str);

The result returns null. C {2,} represents more than 2 C, while there is only 1 C in Cainiao.

As can be seen from the above example, C {n,} represents at least N C, and there is no limit to the number at most.

*,+,?
*Represents 0 or more times, which is equivalent to {0,}, i.e
C * and C {0,} mean the same thing.

+Represents one or more times, which is equivalent to {1,}, i.e
C + and C {1,} mean the same thing.

Finally,? Represents 0 or 1 times, which is equivalent to {0,1}, i.e
c? And C {0,1} mean the same thing.

Greedy and non greedy
People are greedy, so is regular. In example reg = / C {3,4} /; str=’ccccTest’; As we have seen in the example of, when you can match four, regular will never match three. All the regularities described above are like this. As long as they are legal, they will match as many characters as possible, which is called greedy pattern.
If we want regular to match as few characters as possible, we can add a? After the symbol representing the number?. It consists of the following forms:
{n,}?, *?, +?, ??, {m,n}?
Let’s look at an example:

Copy codeThe code is as follows:
reg = /c{1,}?/;
str=’ccccc’;
execReg(reg,str);

The returned result is only one C. Although there are five C’s that can be matched, only one will be matched because the regular expression is a non greedy pattern.
/^Beginning, end$/
^Indicates that only the beginning of the string is matched. Take the following example:

Copy codeThe code is as follows:
reg = /^c/;
STR = ‘vitamin C’;
execReg(reg,str);

The result is null, because the beginning of the string ‘vitamin C’ is not C, so the matching fails.

Copy codeThe code is as follows:
reg = /^c/;
str=’cainiao’;
execReg(reg,str);

This time, C is returned and the matching is successful, because Cainiao starts with C.

In contrast to ^, $only matches the characters at the end of the string. Similarly, see the example:

Copy codeThe code is as follows:
reg = /c$/;
str=’cainiao’;
execReg(reg,str);

Returns null, indicating that the regular expression cannot find C at the end of the string.

Copy codeThe code is as follows:
reg = /c$/;
STR = ‘vitamin C’;
execReg(reg,str);

The result returned this time is C, indicating that the matching is successful.

Point ‘.’
‘.’ matches all characters in the string except the newline \ n, for example

Copy codeThe code is as follows:
reg = /./;
str=’cainiao’;
execReg(reg,str);

The results show that the regular match to the character C.

Copy codeThe code is as follows:
reg = /./;
str=’blueidea’;
execReg(reg,str);

This time it’s B.

Copy codeThe code is as follows:
reg = /.+/;
STR =’blueidea – good classic forum. ‘;
execReg(reg,str);

The result is “blueidea – Classic forum good.” that is, all characters are matched, including a space, a slide line, and a dash.

Copy codeThe code is as follows:
reg = /.+/;
reg = /.+/;
str=’bbs.blueidea.com’;
execReg(reg,str);

Similarly, if you directly return the whole string – bbs.blueidea.com, you can see that “,” also matches “,” itself.

Copy codeThe code is as follows:
reg = /^./;
str=’\ncainiao’;
execReg(reg,str);

The result is null, and finally failed. Regular requires that the first character of the string is not a newline, but exactly the character starts with \ n.

“Or in a regular expression,”|“
B|c means that it matches B or C.
For example:

Copy codeThe code is as follows:
reg = /b|c/;
str=’blueidea’;
execReg(reg,str);

The result is B.

Copy codeThe code is as follows:
reg = /b|c/;
str=’cainiao’;
execReg(reg,str);

The result is C.

Copy codeThe code is as follows:
reg = /^b|c.+/;
str=’cainiao’;
execReg(reg,str);

Match the whole Cainiao.

Copy codeThe code is as follows:
reg = /^b|c.+/;
str=’bbs.blueidea.com’;
execReg(reg,str);

The result is only one B, not the entire string. Because the above regular expression means to match the beginning B or C. +.
brackets

Copy codeThe code is as follows:
reg = /^(b|c).+/;
str=’bbs.blueidea.com’;
execReg(reg,str);

This time, the result is the whole string bbs.blueidea.com. After the brackets on the machine, this regular means that if the beginning of the string is B or C, then match the beginning B or C and all subsequent non newline characters.
If you also experiment, you will find an “B” after the returned result, which is the matching content of b|c in (). What we write in the brackets of the regular expression will be regarded as a sub regular expression, and the matching results will be recorded for later use. We will ignore this feature for the time being.

Character set [ABC]
[ABC] represents any character in a or B or C. For example:

Copy codeThe code is as follows:
reg = /^[abc]/;
str=’bbs.blueidea.com’;
execReg(reg,str);

The return result is B.

Copy codeThe code is as follows:
reg = /^[abc]/;
str=’test’;
execReg(reg,str);

The result this time is null.

We use the following representations in the word character set: [A-Z], [A-Z], [0-9], representing lowercase letters, uppercase letters and numbers respectively. For example:

Copy codeThe code is as follows:
reg = /^[a-zA-Z][a-zA-Z0-9_]+/;
str=’test’;
execReg(reg,str);

The result is the whole test. Regular means that the beginning must be English letters, followed by English letters or numbers and underscores.

Inverse character set [^ ABC]
^At the beginning of a regular expression, it means the beginning. For example, / ^ C / means the beginning is C; However, in character sets and, it means something like “not”. For example, [^ ABC] means that it cannot be any of a, B or C. for example:

Copy codeThe code is as follows:
reg = /[^abc]/;
str=’blueidea’;
execReg(reg,str);

The result returned is l because it is the first non ABC character (that is, the first B does not match). Similarly:

Copy codeThe code is as follows:
reg = /[^abc]/;
str=’cainiao’;
execReg(reg,str);

I is returned, and the first two characters are from the [ABC] set.
From this we can see that: [^ 0-9] represents non numbers, [^ A-Z] represents non lowercase letters, and so on.

Boundary and non boundary
\B means the boundary of the string, that is, only the beginning and end of the string count. For example / \ BC / represents the C at the beginning or the C at the end of the string. Take the following example:

Copy codeThe code is as follows:
reg = /\bc/;
str=’cainiao’;
execReg(reg,str);

Return result C. Matches the C character of the left boundary.

Copy codeThe code is as follows:
reg = /\bc/;
STR = ‘vitamin C’;
execReg(reg,str);

It still returns C, but this time it returns C on the right boundary.

Copy codeThe code is as follows:
reg = /\bc/;
str=’bcb’;
execReg(reg,str);

This match failed because C in the BCB string is sandwiched in the middle, neither in the left boundary nor in the right boundary.

Corresponds to \ B \ B represents a non boundary. For example:

Copy codeThe code is as follows:
reg = /\Bc/;
str=’bcb’;
execReg(reg,str);

This time, it will be successfully matched to C in BCB,. however

Copy codeThe code is as follows:
reg = /\Bc/;
str=’cainiao’;
execReg(reg,str);

Null is returned. Because \ B tells regular to match only non bounded C.

Digital and non digital
\D means number, on the contrary, \ D means non number. For example:

Copy codeThe code is as follows:
reg = /\d/;
str=’cainiao8′;
execReg(reg,str);

The match returned is 8 because it is the first numeric character.

Copy codeThe code is as follows:
reg = /\D/;
str=’cainiao8′;
execReg(reg,str);

Returns C, the first non numeric character.

blank
\F matches page breaks, \ n matches line breaks, \ r matches carriage returns, \ t matches configuration tabs, \ V matches vertical tabs.
\S matches a single space, equivalent to [\ f \ n \ R \ t \ v]. For example:

Copy codeThe code is as follows:
reg = /\s.+/;
str=’This is a test String.’;
execReg(reg,str);

“Is a test string.” is returned. Regular means to match the first space and all subsequent non newline characters.

Similarly, \ s represents a non whitespace character.

Copy codeThe code is as follows:
reg = /\S+/;
str=’This is a test String.’;
execReg(reg,str);

The matching result is this. When the first space is encountered, the regular stop matching.

Word character
\W represents a word character, which is equivalent to the character set [a-za-z0-9_]. For example:

Copy codeThe code is as follows:
reg = /\w+/;
str=’blueidea’;
execReg(reg,str);

Returns the complete blueidea string because all characters are word characters.

Copy codeThe code is as follows:
reg = /\w+/;
str=’.className’;
execReg(reg,str);

The result shows that the classname in the string is matched, and only the first “.” – the only non word character is not matched.

Copy codeThe code is as follows:
reg = /\w+/;
STR = ‘how about Chinese?’;
execReg(reg,str);

Trying to match Chinese characters with word characters naturally doesn’t work. Null is returned.

\W represents a non word character, equivalent to [^ a-za-z0-9]

Copy codeThe code is as follows:
reg = /\W+/;
STR = ‘how about Chinese?’;
execReg(reg,str);

Returns the complete string, because both Chinese and “?” are counted as non word characters.

Back reference
The form is as follows: / (subregular expression) \ 1/
Still use examples to illustrate:
1.

Copy codeThe code is as follows:
reg = /\w/;
str=’blueidea’;
execReg(reg,str);

Return to B.
2.

Copy codeThe code is as follows:
reg = /(\w)(\w)/;
str=’blueidea’;
execReg(reg,str);

Return BL, B, l
BL is the content of the whole regular matching, B is the content of the sub regular expression matching in the first bracket, and l is the content of the second bracket.
3.

Copy codeThe code is as follows:
reg = /(\w)\1/;
str=’blueidea’;
execReg(reg,str);

Null is returned. The “\ 1” here is called a back reference. It represents the content of the word in the first bracket that matches the regular expression. In the above example, the (\ w) in the first bracket matches B, so “\ 1” also represents B. naturally, B cannot be found in the rest of the string.
Compared with the second example, it can be found that “\ 1” is equivalent to “the content matching the first bracket”, rather than “the content of the first bracket”.

Copy codeThe code is as follows:
reg = /(\w)\1/;
str=’bbs.blueidea.com’;
execReg(reg,str);

This regular will match BB.

Similarly, with a few subregular expressions, we can use several backreferences. For example:

Copy codeThe code is as follows:
reg = /(\w)(\w)\2\1/;
str=’woow’;
execReg(reg,str);

The matching is successful because the first bracket matches W, the second bracket matches o, and \ 2 \ 1 represents ow, which exactly matches the last two characters of the string.

Brackets (2)
We discussed the problem of brackets once before. See the following example:

Copy codeThe code is as follows:
reg = /^(b|c).+/;
str=’bbs.blueidea.com’;
execReg(reg,str);

The purpose of this rule is to match only strings starting with B or C until the newline character, but. As we have seen above, you can use “\ 1” to back reference the content matched by the sub regular expression in this bracket. Moreover, the exec method will also save the matching result of the word regular expression to the returned result.
The matching results of sub regular expressions are not recorded
Using a regular pattern like (?: pattern) can avoid saving the matching results in parentheses. For example:

Copy codeThe code is as follows:
reg = /^(?:b|c).+/;
str=’bbs.blueidea.com’;
execReg(reg,str);

You can see that the returned result no longer includes the words in parentheses and many matches in the regular expression.
In the same way, reverse reference is not easy to use:

Copy codeThe code is as follows:
reg = /^(b|c)\1/;
str=’bbs.blueidea.com’;
execReg(reg,str);

Return BB, B. BB is the matching content of the whole regular expression, and B is the matching content of the first sub regular expression.

Copy codeThe code is as follows:
reg = /^(?:b|c)\1/;
str=’bbs.blueidea.com’;
execReg(reg,str);

Returns null. Since there is no record of the matching content in parentheses, there is naturally no way to reverse reference.

Forward pre check
Form: (? = pattern)
The so-called forward pre query means that the string to be matched must be followed by pattern!
We know that the regular expression / Cainiao / will match Cainiao. Similarly, Cainiao in cainiao9 will be matched. However, we may hope that Cainiao can only match the rookies in cainiao8. At this time, you can write as follows: / Cainiao (? = 8) /. Look at two examples:

Copy codeThe code is as follows:
reg = /cainiao(?=8)/;
str=’cainiao9′;
execReg(reg,str);

Returns null.

Copy codeThe code is as follows:
reg = /cainiao(?=8)/;
str=’cainiao8′;
execReg(reg,str);

Match Cainiao.
It should be noted that the contents in parentheses do not participate in the real matching, but just check whether the following characters meet the requirements. For example, the above regular returns Cainiao instead of cainiao8.

Let’s look at two examples:

Copy codeThe code is as follows:
reg = /blue(?=idea)/;
str=’blueidea’;
execReg(reg,str);

Match to blue, not blueidea.

Copy codeThe code is as follows:
reg = /blue(?=idea)/;
str=’bluetooth’;
execReg(reg,str);

Null is returned because blue is not followed by idea.

Copy codeThe code is as follows:
reg = /blue(?=idea)/;
str=’bluetoothidea’;
execReg(reg,str);

Also return null.

?!
Form (?! pattern) and= On the contrary, it is required that the string cannot be followed by a pattern. Take the above example:

Copy codeThe code is as follows:
reg = /blue(?!idea)/;
str=’blueidea’;
execReg(reg,str);

Null is returned. Because of regular requirements, blue cannot be followed by idea.

Copy codeThe code is as follows:
reg = /blue(?!idea)/;
str=’bluetooth’;
execReg(reg,str);
/[code]
Blue is returned successfully.

Match metacharacter
First of all, what are metacharacters? We’ve used *, +,? Such symbols have a certain special meaning in regular expressions. Characters with special functions like these are called metacharacters. for example
reg = /c*/;
Indicates that there is any C, but what if we really want to match the string ‘c *’? Just escape * as follows:
[code]
reg = /c\*/;
str=’c*’;
execReg(reg,str);

Returns the matching string: C *.

Similarly, to match other metacharacters, just add a “\” in front of it.

Modifier of regular expression
Global match, modifier G
Form: / pattern / g
Example: reg = / B / g;
I’ll talk about the role of this g later. Look at the next two modifiers first.
Case insensitive, modifier I
Form: / pattern / I
example:

Copy codeThe code is as follows:
var reg = /b/;
var str = ‘BBS’;
execReg(reg,str);

Returns null because the case does not match.

Copy codeThe code is as follows:
var reg = /b/i;
var str = ‘BBS’;
execReg(reg,str);

Match to B, this is the function of the I modifier.
Line beginning and end, modifier M
Form: / pattern / M
The M modifier modifies the role of ^ and $in regular expressions so that they represent the beginning and end of a line, respectively. For example:

Copy codeThe code is as follows:
var reg = /^b/;
var str = ‘test\nbbs’;
execReg(reg,str);

Matching failed because there is no B character at the beginning of the string. But with the M modifier:

Copy codeThe code is as follows:
var reg = /^b/m;
var str = ‘test\nbbs’;
execReg(reg,str);

Match to B, because ^ represents the beginning of the line after adding the M modifier. Because BBS is at the beginning of the second line of the string, it can be matched successfully.
Detailed explanation of exec method
Return value of exec method
The exec method does not return a matching result string, but an object. Simply modify the execreg function to do an experiment to verify this:

Copy codeThe code is as follows:
function execReg(reg,str){
var result = reg.exec(str);
alert(typeof result);
}
var reg = /b/;
var str=’bbs.bblueidea.com’;
execReg(reg,str);

The result shows that the type of result is object. And it is an array like object. Use for in to know its attribute: index input 0. Where index is the index matching in the original string; Input is the string representing the input;
As for 0, it means that there is only one matching result. You can use the subscript 0 to refer to the matching result. This number may change. We can know the total number of matching results by the length attribute of the return value.
According to the above analysis of the return value, modify the execreg function as follows:

Copy codeThe code is as follows:
function execReg(reg,str){
var result = reg.exec(str);
document.write(‘index:’+result.index+'<br />’
+’input:’+result.input+'<br />’
);
for(i=0;i<result.length;i++){
document.write(‘result[‘+i+’]:’+result[i]+'<br />’)
}
}

Experiment now:

Copy codeThe code is as follows:
var reg = /\w/;
var str=’bbs.bblueidea.com’;
execReg(reg,str);

The results are as follows:
index:0
input:bbs.bblueidea.com
result[0]:b
The input string is bbs.bblueidea.com;
The index of the matching B in the original string is 0.
The regular matching result is a, B;

Copy codeThe code is as follows:
var reg = /(\w)(\w)(.+)/;
var str=’bbs.bblueidea.com’;
execReg(reg,str);

The result is:
index:0
input:bbs.bblueidea.com
result[0]:bbs.bblueidea.com
result[1]:b
result[2]:b
result[3]:s.bblueidea.com

As can be seen from the above two examples, the returned object [0] is what the entire regular expression matches. The subsequent elements are the matching contents of each sub regular expression.

Update of regular expression by exec method
While the exec method returns the result object, it may also update the original regular expression, depending on whether the G modifier is set in the regular expression. Let’s start with two examples:

Copy codeThe code is as follows:
var reg = /b/;
var str = ‘bbs.blueidea.com’;
execReg(reg,str);
execReg(reg,str);

The results are as follows:
index:0
input:bbs.blueidea.com
result[0]:b
index:0
input:bbs.blueidea.com
result[0]:b
In other words, the results of the two matches are exactly the same. As can be seen from the index, the matching is the B character at the beginning of the string.
Let’s take a look at the performance of regular expressions with G set:

Copy codeThe code is as follows:
var reg = /b/g;
var str = ‘bbs.blueidea.com’;
execReg(reg,str);
execReg(reg,str);

The results are as follows:
index:0
input:bbs.blueidea.com
result[0]:b
index:1
input:bbs.blueidea.com
result[0]:b
It can be seen that the second match is the second B of the string. This is the role of the G modifier. Let’s see how exec treats g and non G regular expressions differently.

If G is not set for the regular expression, the exec method will not have any impact on the regular expression. If G is set, the lastindex property of the regular expression will be updated after exec execution, indicating the index of the next character of the matched string after this matching, The next time you use this regular expression to match a string, you will start matching from the lastindex attribute last time, which is why the results of the above two examples are different.

Test method
The test method simply checks whether STR can be matched and returns a Boolean value indicating success. Similarly, establish a simple test function:

Copy codeThe code is as follows:
function testReg(reg,str){
alert(reg.test(str));
}

Example 1

Copy codeThe code is as follows:
var reg = /b/;
var str = ‘bbs.blueidea.com’;
testReg(reg,str);

Success, output true.
Example 2

Copy codeThe code is as follows:
var reg = /9/;
var str = ‘bbs.blueidea.com’;
testReg(reg,str);

Failed, return false.
Execute regular expressions using string methods
Match method
Form: str.match (reg);
Similar to the exec method of regular expression, this method also returns an array like object with input and index attributes. We define the following function to test:

Copy codeThe code is as follows:
function matchReg(reg,str){
var result = str.match(reg);
if(result ){
document.write(‘index:’+result.index+'<br />’
+’input:’+result.input+'<br />’
);
for(i=0;i<result.length;i++){
document.write(‘result[‘+i+’]:’+result[i]+'<br />’)
}
}else{
Alert (‘null: matching failed! ‘)
}
}

For example:

Copy codeThe code is as follows:
var reg = /b/;
var str = ‘bbs.blueidea.com’;
matchReg(reg,str);

The results are as follows:
index:0
input:bbs.blueidea.com
result[0]:b
It can be seen that the result is the same as that of exec.
However, if the G modifier is set in the regular expression, the behavior of exec and match will be different, as shown in the following example:
index:undefined
input:undefined
result[0]:b
result[1]:b
result[2]:b
Regular expressions with the G modifier set will not stop after a successful match, but continue to find all characters that can be matched. The returned result includes three B’s. However, input and index are not provided.

Replace method
Form: str. replace (reg, ‘new STR’);
Its function is to use the “new STR” part of the code to match reg in the str string. It is worth noting that the original string will not be modified, but will be returned as a return value. example:

Copy codeThe code is as follows:
var reg = /b/;
var str = ‘bbs.blueidea.com’;
var newStr = str.replace(reg,’c’);
document.write(newStr);

The result is cbs.blueidea.com, and only the first B is replaced by C.

Copy codeThe code is as follows:
var reg = /b/g;
var str = ‘bbs.blueidea.com’;
var newStr = str.replace(reg,’c’);
document.write(newStr);

Output ccs.clueidea.com
Since the G modifier is set, all B’s are replaced.

Copy codeThe code is as follows:
var reg = /\w+/g;
var str = ‘bbs.blueidea.com’;
var newStr = str.replace(reg,’word’);
document.write(newStr);

Output:
word.word.word。
Use $in the replace function to refer to the sub regular expression matching content
Just as we can use \ 1 to refer to the content matched by the first sub regular expression in the regular expression, we can also use $1 to refer to the same content in the replacement character of the replace function.
Let’s take an example:

Copy codeThe code is as follows:
var reg = /(\w+).(\w+).(\w+)/;
var str = ‘bbs.blueidea.com’;
var newStr = str.replace(reg,’$1.$1.$1′);
document.write(newStr);

The output results are:
bbs.bbs.bbs
First, we know that the first sub regular expression matches the BBS, so $1 represents the BBS. Then we set the replacement string to ‘$1. $1. $1’, which is actually “BBS. BBS. BBS”. Similarly, $2 is blueidea and $3 is com.

Let’s look at an example. Reverse the order of two words before and after the space.

Copy codeThe code is as follows:
var reg = /(\w+)\s(\w+)/;
var str = ‘cainiao gaoshou’;
var newStr = str.replace(reg,’$2 $1′);
document.write(newStr);

The result is: Gaoshou Cainiao, that is, the words before and after the space are reversed.

Since $has a special meaning in the replacement text, if we want to use the character $, we need to write it as $$, for example:

Copy codeThe code is as follows:
var reg = /(\w+)\s(\w+)/;
var str = ‘cainiao gaoshou’;
var newStr = str.replace(reg,’$$ $$’);
document.write(newStr);

The result is: $.

Search method and split method
Similarly, regular expressions can also be used in the search method and split method of string, as follows:
str.search(reg);
Search returns the position where the regular expression first matches. example:

Copy codeThe code is as follows:
var reg = /idea/;
var str = ‘blueidea’;
var pos = str.search(reg);
document.write(pos);

The result is 4.
The following example finds the first non word character:

Copy codeThe code is as follows:
var reg = /\W/;
var str = ‘bbs.blueidea.com’;
var pos = str.search(reg);
document.write(pos);

The result is 3, which is the position of the dot “.”.

Copy codeThe code is as follows:
str.split(reg,’seprator’);
Split returns the split array, for example:
var reg = /\W/;
var str = ‘bbs.blueidea.com’;
var arr = str.split(reg);
document.write(arr);

The results are: BBS, blueidea, com. The visible array is divided into three elements by non word characters.

Copy codeThe code is as follows:
var reg = /\W/;
var str = ‘http://www.baidu.com/’;
var arr = str.split(reg);
document.write(arr.length+'<br />’);
document.write(arr);

The result is:
7
http,,,www,baidu,com,
The visible string is divided into an array of seven elements, including three elements that are empty strings.

12 read the full text on the next page