preface

Regular expressions are very common in everyday work, but their magic may have long been like magic for many programmers. Most of the regular expressions used in work are obtained by searching the Internet, which is more complicated and time-consuming to put together by looking at documents. Are you impressed? Once I saw a video about regularity on the Internet, which made me receive a lot of goods. At that time, I carefully recorded my notes and my feelings, hoping to bring help to more children’s shoes in need. The article starts from scratch to talk about regular, resulting in a bit long, can collect fragmentary time slowly look, seriously read the absolute benefit. The article was first posted on my blog. Here is the text:

Regular Expression Uses a single string to describe and match a series of strings that conform to a certain syntax rule. In simple terms, it is to follow some rule to match the string that matches the condition. Here’s a good online tool for learning regular expressions: regexper.com/, which uses images and English solutions… ^\d{4}[/-]\d{2}[/-]\d{2}$

Isn’t that intuitive

The RegExp object

JavaScript supports regular expressions through the built-in object RegExp. There are two ways to instantiate a RegExp object:

  1. literal
  2. The constructor

1. Literal

Suppose you need to match the lowercase is in English with the uppercase is, you can do this:

var reg = /\bis\b/;
var text = 'He is a boy, This is a dog. Where is she? ';
var result = text.replace(reg,'IS');
console.log(result) //He IS a boy, This is a dog. Where is she?
Copy the code

This replaces the first English word ‘is’ with ‘is’. If you wanted to replace all the words ‘is’ in the sentence with ‘is’, you would write:

var reg = /\bis\b/g;
var text = 'He is a boy, This is a dog. Where is she? ';
var result = text.replace(reg,'IS');
console.log(result) //He IS a boy, This IS a dog. Where IS she?
Copy the code

Just add a ‘g’ to the end of the re, which stands for global match. ‘g’ is a regular expression modifier. The modifier is:

  • ‘g’: Global full text search, not added, search to the first stop
  • ‘I ‘: Ignore case ignores case. Default is case sensitive
  • ‘m’: multiple line search, detects newline characters in string, mainly affecting the use of the string start identifier ^ and end identifier $

You may be wondering why the is in the sentence ‘This’ is not matched, because we ‘\b’ did it.

‘\b’: Matches a word boundary, that is, the position between a word and a space. For example, “er\b” can match the “er” in “never”, but not the “er” in “verb”.

The re here has a ‘\b’ before and after the ‘is’, which only matches the word ‘is’.

The constructor

If you need to use the constructor to instantiate the re, the above literal form could look like this:

var reg = new RegExp('\\bis\\b'.'g');
var text = 'He is a boy, This is a dog. Where is she? ';
var result = text.replace(reg,'IS');
console.log(result) //He IS a boy, This IS a dog. Where IS she?
Copy the code

In this way there is no need for the ‘/’ symbol to start and end to indicate that it is regular. But special characters such as ‘\’ need to be escaped with ‘\’.

‘\’: marks the next character as a special character, or a literal character, or a backreference, or an octal escape. For example, “n” matches character “n”. “\n” matches a newline character. The serial “\\” matches “\” and “\(” matches” (“.

metacharacters

Regular expressions consist of two basic character types:

  • A literal character is a character that represents its original meaning
  • Metacharacters are non-alphabetic characters that have special meanings in regular expressions. For example, ‘\b’ is used to match word boundaries, not ‘\b’. These special characters mainly exist in regular expressions:*, +,? , $^,., |, \, (,), {and}, [and]

Character classes (character collections)

In general, a regular expression corresponds to a string with one character. For example, ab\t matches the string ‘ab’+’ TAB ‘.

But more often than not, we match not a character, but a string that matches a set of characteristics. In this case, we can use the metacharacter ‘[]’ to build a simple class. A class is an object that conforms to some property, a general character, not a character in particular. For example, the expression ‘[ABC]’ groups characters a or B or C, and the expression can match such characters.

var reg = /[abc]/g;
var text = 'a1b2c3d4';
var result = text.replace(reg,'X');
console.log(result); //X1X2X3d4
Copy the code

So instead of matching three characters like ABC, we’re matching any character in ABC, which is an application of metacharacters.

The character class is inverted

Use the metacharacter ‘^’ to create a reverse/negative class.

Reverse class means something that is not part of a class, and the expression ‘[^ ABC]’ means something that is not a character a or B or C, for example:

var reg = /[^abc]/g;
var text = 'a1b2c3d4';
var result = text.replace(reg,'X');
console.log(result); //aXbXcXXX
Copy the code

Scope of the class

If we need to match a number with a character class, it might be difficult to write the same way as before: ‘[0123456789]’, especially for a to Z characters.

To do this, regular expressions give us range classes where we can use [a-z] to concatenate two characters, representing any character from A to Z, which is a closed interval containing a and z themselves.

var reg = /[a-z]/g;
var text = 'a1b2c3d4z9';
var result = text.replace(reg,'Q');
console.log(result); //Q1Q2Q3Q4Q9
Copy the code

Can be found, this is a lot more convenient.

In addition, inside the class formed by ‘[]’ is the conjoined [a-za-z], thus forming uppercase letters and lowercase letters matching exactly:

var reg = /[a-zA-Z]/g;
var text = 'a1b2c3d4z9ASDFHDFH';
var result = text.replace(reg,'Q');
console.log(result); //Q1Q2Q3Q4Q9QQQQQQQQ
Copy the code

Some kids might be thinking, I want to match the ‘-‘ character in the range class, what do we do?

It’s actually quite simple, for example:

var reg = /[0-9]/g; // This is the same result as before, no
var text = '2018-05-13';
var result = text.replace(reg,'Q');
console.log(result); //QQQQ-QQ-QQ

var reg = /[0-9-]/g; // Just add another '-' after it
var text = '2018-05-13';
var result1 = text.replace(reg,'Q');
console.log(result1); //QQQQQQQQQQ
Copy the code

Predefined classes and boundaries

Predefined classes

Regular expressions provide pre-defined classes to match common character classes, making it easier to write.

character Equivalence class meaning
. [^\r\n] All characters except carriage returns and newlines
\d [0-9] Numeric characters
\D [^ 0-9] Non-numeric character
\s [\t\n\x0B\f\r] Whitespace characters
\S [^\t\n\x0B\f\r] Non whitespace characters
\w [a-zA-Z_0-9] Word characters (letters, numbers, underscores)
\W [^a-zA-Z_0-9] Non-word character

Digit ‘\d’, space blank ‘\s’, word letter ‘\w’, uppercase, mom no longer need to worry that I remember wrong

Let’s look at a practical example that matches a string ab+ a number + any character:

var reg = /ab\d./; // Before we might have written: ab[0-9][^\r\n]
var text = 'absdlkjflab91323';
var result = text.replace(reg,'AAAA');
console.log(result); //absdlkjflAAAA323
Copy the code

The border

In addition to predefined classes, regular expressions provide several commonly used boundary characters.

character Equivalence class
^ Start with XXX
$ Ended up with XXX
\b Word boundaries
\B Non-word boundary

We used the ‘\b’ word boundary in the first example. Here we do the opposite, replacing only the ‘is’ in ‘This’ with the ‘is’.

var reg = /\Bis\b/g;
var text = 'He is a boy, This is a dog. Where is she? ';
var result = text.replace(reg,'IS');
console.log(result) //He is a boy, ThIS is a dog. Where is she?
Copy the code

Then we can talk about ‘^’ and ‘$’. In class ‘[]’, ‘^’ means inverse, but when not in class, ‘^’ means start with XXX and ‘$’ means end with XXX. These boundary characters are usually placed at the beginning and end of the re.

// let's see if we don't add ^ or $
var reg = /@./g;
var text = '@123@ab@A';
var result = text.replace(reg,'Q');
console.log(result); //Q23QbQ

// Add ^ case
var reg = /^@./g;
var text = '@123@ab@A';
var result1 = text.replace(reg,'Q');
console.log(result1); //Q23@ab@A

// Add $
var reg = /@.$/g;
var text = '@123@ab@A';
var result1 = text.replace(reg,'Q');
console.log(result1); //@123@abQ
Copy the code

In the above example, if both ‘^’ and ‘$’ are added, the match will not be successful, because there is no sign of the string to match, you can try it yourself.

Here is an example combining multi-line matching:

var reg = /^@\d./g;
var text= '@123\n@456\n@789';
var result = text.replace(reg,'Q');
console.log(result);// Q3 @456 @789
Copy the code

Here you can see that instead of replacing all three lines as expected, only the first line has been replaced. Why is that?

This is because a newline is what we think of as a new line, but when a program processes a string, a newline is an ordinary character, not a new line. This is where our modifier ‘m’ comes into play:

var reg = /^@\d./gm;
var text= '@123\n@456\n@789';
var result = text.replace(reg,'Q');
console.log(result);// Q3 @6 @9
Copy the code

quantifiers

If we wanted to match a string of numbers that appear 20 times in a row, we might write 20 ‘\d’ in a row, based on what we learned earlier. If 20 times is ok with you, what do you do 100 times, 1,000 times, or more?

To solve this problem, regular expressions introduce the concept of quantifiers. Here are some quantifiers and what they mean:

character meaning
? Zero or one occurrence (maximum one occurrence)
+ Appear once or more (at least once)
* Zero or more occurrences (any occurrence)
{n} A n time
{n,m} N to m occurrences
{n,} At least n times

We can take the beginning date of the article as an example:

var reg = /\d{4}[/-]\d{2}[/-]\d{2}/g;
var text = 2018-02-23, 2018/02/24, 2018-02/25 ';
var result = text.replace(reg,'Match the correct date format');
console.log(result);// Match the correct date format, match the correct date format, 2018 to 02/25
Copy the code

Greed mode

Regular expressions default to greedy mode, that is, they match as many characters as possible at a time until the match fails.

Here’s an example:

var reg = / \ d {3, 6} / g;
var text = '12345678';
var result = text.replace(reg,'X');
console.log(result);//X78
Copy the code

As you can see from the above, the regular expression matches ‘123456’, not ‘123’,’1234′,’12345′, as many times as possible, namely greedy mode.

What if we want it to only match 3 times, as few matches as possible, and once it does, we don’t try any more, so what do we need to do in non-greedy mode?

It’s very simple. After the quantifier you add ‘? Let’s try the example again:

var reg = / \ d {3, 6}? /g;
var text = '12345678';
var result = text.replace(reg,'X');
console.log(result);//XX78
Copy the code

grouping

If we have a scenario that matches the string Byron 3 times in a row, we might write Byron{3}.

If Byronnn repeats the last n three times, it doesn’t match the whole word three times:

var reg = /Byron{3}/g;
var text = 'ByronByronByronnn';
var result = text.replace(reg,'0');
console.log(result);//ByronByron0
Copy the code

So how do we match Byron’s 3 occurrences in a row? In this case, the regular expression group ‘()’ helps us solve this problem:

var reg = /(Byron){3}/g;
var text = 'ByronByronByronnn';
var result = text.replace(reg,'0');
console.log(result);//0nn
Copy the code

or

Sometimes, we may need to use or when matching relations, use before ‘[]’ character classes (character sets) may only be able to match a single character or relationships, such as matching a or b, you can write like this: ‘[ab], but if you need to match a whole word or relationship, may not make the’ [] ‘. At this time, we can achieve the result of or use ‘|’ :

// Match words Byron or Casper
var reg = /Byron|Casper/g;
var text = 'ByronCasper'
var result = text.replace(reg,'X');
console.log(result);//XX

// Matches Byr+ ON or Ca+ SPer
var reg = /Byr(on|Ca)sper/g;
var text = 'ByronsperByrCasper'
var result1 = text.replace(reg,'X');
console.log(result1);//XX
Copy the code

backreferences

If we had a requirement to replace the date ‘2015-12-25′ with ’12/25/2015’, what would you do now?

You might write something like:

var reg = /\d{4}-\d{2}-\d{2}/g;
var text = '2015-12-25'
var result = text.replace(reg,'12/25/2015');
console.log(result);/ / 12/25/2015
Copy the code

However, you can only match ‘2015-12-25’, you can’t match any other date, ‘2015-12-25’ will change, so there is no need.

At this point, the re’s backreference comes into play. When an expression matches, the expression engine records (captures) the string that matches the expression contained in the parentheses “()”. The string matched by the expression contained in the parentheses can be retrieved separately.

In JS, the string that matches successfully can be represented by $1 for the first match, $3 for the third match, and so on up to $99). Thus, the above example could be written like this:

var reg = /(\d{4})-(\d{2})-(\d{2})/g;
var text = '2015-12-25'
var result = text.replace(reg,'$2 / $3 / $1');
console.log(result);/ / 12/25/2015
Copy the code

Ignore the grouping

In the backreference above, we default to all capture records from $1 to $99 based on ‘()’. What if we want to ignore a capture?

You don’t want to capture certain groups, just add ‘? :’ will do.

var reg = / (? :Byron)(\d{4})-(\d{2})-(\d{2})/g;
var text = 'Byron2016-12-05'
var result = text.replace(reg,'$2 / $3 / $1');
console.log(result);/ / 12/05/2016
Copy the code

Now $1 isn’t Byron, it’s 2016.

foresight

A regular expression is parsed from the beginning of the text to the end of the text. The end direction of the text is called “forward”. When a regular expression matches a rule, the forward direction checks whether the rule matches the assertion, and the backward direction checks whether the rule matches the assertion.

Coincidence and noncoincidence assertions are called positive/positive matching and negative/negative matching.

Above is the concept of foresight, is it a little dizzy after reading? I was a little dizzy… Let me explain: If you need to match a name is “zhang”, before we had a bunch of people find out name “zhang” is out of line, but the vision is to ask you, find out the name of the person who is called “zhang” is not enough, must also “zhang SAN” father must be a “second” or other specific conditions, so is the foresight, similarly, Hou Zhan is the name of Zhang SAN is not enough, the son must be called “xiao Zhang” and so on.

Does that make sense? Note, however, that backward-looking is not supported in javascript, so we don’t need to care. (Correction: in ES2018(ES9), look-forward and named groupings are already supported.)

As for the match/dismatch assertion, it can be interpreted as: for example, the match request name is “Zhang SAN”, and his father is not “Zhang Er”. For the match we call positive/positive match, and for the non-match we call negative/negative match.

Let’s use the table again to illustrate:

The name of the regular meaning
Positive predictive exp(? =assert) We match the exp part of the expression, and then we must also match the assertion part (inside ‘()’, the re after ‘=’)
Negative predictive exp(? ! assert) We match the exp part of the expression, and then we have to match the assertion part (‘()’ inside, ‘! ‘after the re), is considered successful
Is long exp(? <=assert) Javascript does not support
Negative to look exp(? <! assert) Javascript does not support

Is it clear now? If that doesn’t make sense, let’s do another example:

var reg = /\w(? =\d)/g;
var text = 'a2*3';
var result = text.replace(reg,'X');
console.log(result);//X2*3
Copy the code

Note that we assert that the content is only one of the conditions for matching, which is a necessary condition, but the nature of matching is to match only the re before “()”, so the result above is: ‘X2*3’, not ‘X*3’. Var reg = /\w\d/g; var reg = /\w\d/g; Isn’t it?

Object properties

When we use regular expression related methods, we often use some regular expression related object properties. Here we summarize the regular expression related object properties:

  • Golbal: Indicates whether to search for the full text. The default value is false
  • Ignore case: whether the case is case sensitive. The default value is false
  • Multiline: multi-line search, false by default
  • LastIndex: is the position next to the last character of the current expression matching content
  • Source: Text string of the regular expression

We have already mentioned the first three of them in the previous article, and let’s combine them together to see the code:

var reg1 = /\w/;
var reg2 = /\w/gim;

console.log(reg1.global);//false
console.log(reg1.ignoreCase);//false
console.log(reg1.multiline);//false

console.log(reg2.global);//true
console.log(reg2.ignoreCase);//true
console.log(reg2.multiline);//true

console.log(reg1.source);//\w
console.log(reg2.source);//\w
Copy the code

Golbal, ignore case, and multiline are all false by default, and source is the regular string text you write.

As for lastIndex, let’s start with an example:

var reg1 = /\w/;
var reg2 = /\w/g;

console.log(reg1.test('a'));//true
console.log(reg1.test('a'));//true
console.log(reg1.test('a'));//true
/ /... True no matter how many times it is executed

console.log(reg2.test('ab'));//true
console.log(reg2.test('ab'));//true
console.log(reg2.test('ab'));//false
console.log(reg2.test('ab'));//true
console.log(reg2.test('ab'));//true
console.log(reg2.test('ab'));//false
/ /... Loop true true false
Copy the code

Strange, isn’t it? That’s what ‘lastIndex’ does. (For the test method, double down.) Let’s print ‘lastIndex’ :

var reg2 = /\w/g;
while(reg2.test('ab')) {console.log(reg2.lastIndex); // 1   2
}
Copy the code

As you can see,’ lastIndex’ is constantly changing, that is, the position next to the last character of the current match, where the ‘A’ character is matched for the first time, the last character of the match is also ‘A ‘, and the index next to the’ A ‘character is 1. Similarly, a second match of ‘b’ will result in the last character of the match being ‘b’, and the index at the next position of the ‘b’ character will be 2. Now look at the concept, do you understand a lot?

Therefore, the regex does not start each match from scratch. Instead, the regex looks back from the last match to see if there is more to match. If there is more to match, this must be based on the ‘G’ global match, otherwise every match will start from scratch.

The regular expression RegExp object itself

The RegExp object has three built-in methods:

  1. Test: Retrieves the value specified in the string. Returns true or false
  2. Exec: Retrieves the value specified in the string. Returns the value found and determines its position
  3. Compile: regular expressions (not commonly used)

The test method

The test() method tests whether a string matching the regular expression pattern exists in a string argument, returning true if it exists and false otherwise.

The syntax is regexpobject.test (string), which returns true if the string string contains text matching RegExpObject, and false otherwise.

Here’s an example:

var str = "good good study, day day up";
var reg = new RegExp("study");
var result = reg.test(str);
console.log(result);//true
Copy the code

The exec method

The exec() method is used to perform a search on the string using the regular expression pattern and updates the properties of the global RegExp object to reflect the match.

The syntax is regexpobject.exec (string), and if the string string contains text matching RegExpObject, an array is returned containing the matching results. If no match is found, the return value is null.

And the array has two additional attributes:

  1. Index: Declares the position of the first character of the matching text
  2. Input: Stores the retrieved String String

The exec() method is more complex, with different results for global and non-global calls:

Non-global (i.e., without ‘g’) call:

  • An array is returned when exec() of a non-global RegExp object is called
  • The first element in the array is the text that the regular expression matches
  • The second element in the array is the text (if any) that matches the first subexpression of RegExpObject
  • The third element in the array is the text that matches the second subexpression of the RegExp object (if any, and so on)

Do you look a little dizzy again? No, let’s look at an example:

var reg3 = /\d(\w)\d/;
var str = '1a2b3c4d5e';
var arr = reg3.exec(str);
console.log(reg3.lastIndex + '\t' + arr.index + '\t' + arr.toString());//0	0	1a2,a
Copy the code

That is, the output result is: LastIndex (0 here because lastIndex doesn’t actually work without a global match), the position of the first character of the match text (the extra attribute index, where the first character is ‘1’), and the array of match results (‘ 1A2 ‘is the match text, ‘a’ is the match result of the subexexpression ‘(\w)’, There is no second subexpression ‘()’, so there is no value for the third matching result in the array. Is that a lot clearer?

Global call:

  • Exec () begins retrieving the string at the character specified in the lastIndex attribute of RegExpObject.
  • When exec() finds the text that matches the expression, after the match, it sets the lastIndex property of RegExpObject to the position next to the last character of the matched text.
  • That is, you can iterate over all the matching text in the string by calling the exec() method repeatedly.
  • When exec() finds no more matching text, it returns NULL and resets the lastIndex attribute to 0.
  • Note: If you want to start retrieving new strings after a pattern match in a string, you must manually reset the lastIndex attribute to 0.

Let’s do the same thing:

var reg4 = /\d(\w)(\w)\d/g;
var str = '$1az2bb3cy4dd5ee';

while(arr = reg4.exec(str)){
  console.log(reg4.lastIndex + '\t' + arr.index + '\t' + arr.toString());
  //5 1 1az2,a,z
  //11 7 3cy4,c,y
}
Copy the code

I will not explain the meaning of the results here, I believe that children can understand the global situation after reading the above non-global explanation.

The compile method

The compile() method is used to compile regular expressions during script execution, and can also be used to change and recompile regular expressions. We don’t usually use this in our work (I haven’t seen it yet…). ;

The syntax is regexpobject.compile (regexp,modifier), where ‘modifier’ indicates the regular expression and ‘modifier’ indicates the specified matching type.” G “is used for global matching,” I “is case sensitive, and “gi” is used for global case sensitive matching (excerpt from W3C).

Let’s look at an example:

var str = "Every man in the world! Every woman on earth!";
var reg = /man/g;
var str2 = str.replace(reg,"person");
console.log(str2)

var reg2=/(wo)? man/g;
reg.compile(reg2);
console.log(reg.source);//(wo)? Man: we can see that reg is compiled to reg2 with compile
var str2=str.replace(reg,"person");
console.log(str2);
Copy the code

Search the string globally for “man” and replace it with “person”. Then, with the compile() method, we change the regular expression reg to reg2, and continue to use the reg to replace “man” or “woman” with “person”.

As for why we use compile dynamic correction, what’s the difference between that and creating a new one?

If a regular expression is to be used several times, compiling the regular expression will make the code more efficient, but if it is only executed once or a few times, it will not have a significant effect. Compile improves the adaptability of regular expressions.

String methods that support regular expressions

Regular expressions are supported by String methods:

  • Search: Retrieves the value that matches the regular expression
  • Match: Matches one or more regular expressions.
  • Replace: Replaces the substring that matches the regular expression.
  • Split: Splits a string into an array of strings.

search

The search() method is used to retrieve a substring specified in a string, or to retrieve a substring that matches a regular expression

The syntax is stringobject.search (regexp), which returns index, the starting position of the first substring in stringObject that matches regexp, or -1 if no matching substring is found.

Note that the search() method does not perform a global match, ignores the modifier ‘g’ and always retrieves from the beginning of the string.

Here’s an example:

var str = 'a1b2c3d4';
console.log(str.search('1')); / / 1
console.log(str.search('10')); / / 1
console.log(str.search(/b2/)); / / 2
console.log(str.search(/\w\d/g)); / / 0
console.log(str.search(/\w\d/g)); //0 'g' is ignored and no different result is returned
Copy the code

match

The match() method retrieves the string to find one or more text that matches the RegExp, and whether RegExp has the modifier ‘g’ in it makes a big difference. This method is similar to indexOf() and lastIndexOf(), but it returns the specified value rather than the position of the string.

The syntax is stringobject. match(searchValue) or stringobject. match(regexp), which returns an array of matching results. The contents of the array depend on whether regexp has the global flag G.

The match() method also has global and non-global calls:

Non-global call

  • If regexp does not flag g, then the match() method can perform a match only once in the string
  • If no matching text is found, null is returned
  • Otherwise it returns an array containing information about the matching text it finds:
  • The 0th element of the array holds the matching text, while the remaining elements hold the text that matches the subexpression of the regular expression. (This is similar to the exec() method described earlier)

Also, arrays have two additional attributes (essentially the same as the exec() method) :

  1. Index: Declares the position of the first character of the matching text
  2. Input: Declares a reference to stringObject

Here are some examples:

var reg3 = /\d(\w)\d/;
var str = '1a2b3c4d5e';
var arr = str.match(reg3);
console.log(reg3.lastIndex + '\t' + arr.index + '\t' + arr.toString());//0 0 1a2,a
Copy the code

You can see that the result is the same as the exec() method, except that the positions of the string and re have been swapped.

The global call

Global calls are different from exec() :

  • If regexp has the flag G, the match() method performs a global search to find all matching substrings in the string
  • Returns null if no matching substring is found
  • If one or more matching strings are found, an array is returned
  • Array elements hold all the matching strings in the string, and have no index or input attributes

Simply put, it returns an array containing all the matching results.

var reg4 = /\d(\w)(\w)\d/g;
var str = '$1az2bb3cy4dd5ee';
var arr = str.match(reg4)
console.log(arr); // ["1az2", "3cy4"]
console.log(reg4.lastIndex + '\t' + arr.index) //0	undefined
Copy the code

As you can see, the match() method does not return as much variety of information as the exec() method, but the match() method is more efficient if only the result array is used.

split

I won’t go into detail about the split() method, which we often use to split strings into arrays.

var str = 'a,b,c,d';
var arr = str.split(', ');
console.log(arr); //['a','b','c','d']
Copy the code

But what you may not know is that we can solve some complicated cases using regular expressions

var str = 'a1b2c3d';
var arr = str.split(/\d/);
console.log(arr); //['a','b','c','d']
Copy the code

You might not have seen the point of spilt() splitting with the regular method, but what about more complex splitting, such as:

var str = 'a1b&c|d&e';
var arr = str.split(/[\d|&]/);
console.log(arr); //['a','b','c','d','e']
Copy the code

Does this see the advantage of using re?

Fact: when we use split() to split the character ‘,’, split() converts ‘,’ hermit to re ‘/,/’, search() is the same as replace().

replace

The replace() method is used to replace some characters in a string with other characters, or to replace a substring that matches a regular expression.

Syntax for the stringObject. Replace (regexp/substr, replacement), the result returns a new string, is replacing the regexp with replacement after the first match or all match.

For the replace() method, it can be used in three ways:

  1. String.prototype.replace(str,replaceStr);
  2. String.prototype.replace(reg,replaceStr);
  3. String.prototype.replace(reg,function);

I won’t give you any more examples of the 1 and 2 uses of replace(). If you read the previous article, you’ll probably be familiar with the 1 and 2 uses of replace(). Here’s the third way to use it.

String.prototype.replace(reg,function); Function is called every time a match is made, and has four parameters (the second parameter is not fixed) :

  1. Match string
  2. If the regular expression is not grouped, this parameter is not available.
  3. Index of the match in the string
  4. The original string

As usual, here are two chestnuts:

var str = 'a1b2c3d4e5';
var reg = /\d/g;
var arr = str.replace(reg,function(match, index, origin){
  console.log(index);// 1 3 5 7 9
  return parseInt(match) + 1;
})
console.log(arr);//a2b3c4d5e6 replaces each matched result with +1


var str = 'a1b2c3d4e5';
var reg = /(\d)(\w)(\d)/g;
var arr = str.replace(reg,function(match, group1, group2, group3, index, origin){
  console.log(match);// 1b2 3d4
  return group1 + group3;
})
console.log(arr);//a12c34e5 removes group2 for each match
Copy the code

This is the end of the article, each example is the actual test output, code for a long time…… Hope this article is helpful to you, I am satisfied ~~