JavaScript regular expression
This is an ordinary tutorial, as well as my notes. The reason is that it is very interesting to see another solution to a problem, and at the same time, make some regular notes for easy understanding.
subject
My impression of this question is:
There is an array of [1,1,2,3,3,3,3,4,5,5,5,6,6]
Using js
Turn it into [[1,1],2,[3,3,3],4,[5,5,5]. [6,6]]
There are many solutions. I only list two
Solution 1
Our usual solution is:
Array nesting, put the array with the same value into a new array
Then, the number of nested arrays in the new array is determined and the value is returned to form a new array
The new array is the answer
var arr = [1,1,2,3,3,3,3,4,5,5,5,6,6]; var tempArr = []; var result = []; var i,len,item,lastArr; for(i = 0,len = arr.length;i < len;i++){ item = arr[i]; lastArr = tempArr.slice(-1)[0]; if(!lastArr || lastArr[0] != item){ lastArr = []; tempArr.push(lastArr); } lastArr.push(item); } for(i=0,len=tempArr.length;i<len;i++){ item = tempArr[i]; result.push(item.length > 1 ? item : item[0]); } console.log(result);
Solution 2
If we use regularization, the solution is as follows:
var arr = [1,1,2,3,3,3,3,4,5,5,5,6,6]; var result = "[" + (arr.toString() + ",") .replace(/(([^,]+,)\2+)/g,'[$1],') .replace(/,(]|$)/g,'$1') + "]"; console.log(JSON.parse(result));
In this way, it becomes very efficient. Regularly find out more than two positions with the same value, and insert []
Then print it out.
From this code, we can see that,
First string the array
var result = "[" + (arr.toString() + ",") + "]";
become [1,1,2,3,3,3,3,4,5,5,5,6,6,]
Then match two to be the same value with each other []
var result = "[" + (arr.toString() + ",") .replace(/(([^,]+,)\2+)/g,'[$1],') + "]";
Finally, find ]
Surplus in front ,
var result = "[" + (arr.toString() + ",") .replace(/(([^,]+,)\2+)/g,'[$1],') .replace(/,(]|$)/g,'$1') + "]";
JSON.parse(result)
Convert to array object and print it
regular expression
Through Solution 2, we sometimes face some problems, but there are simpler solutions
Now let's add some regular knowledge points
Metacharacter
from .replace(/(([^,]+,)\2+)/g,'[$1],')
In, the regular part is /(([^,]+,)\2+)/g
The first "/" and the last "/" are separators, representing the beginning and end of the regular expression
Here is a description of metacharacters
code | meaning |
---|
. | Matches any character except line feed |
\w | Match letters or numbers or underscores or Chinese characters |
\W | Match any character that is not a letter or number or an underscore or a Chinese character |
\s | Match any whitespace |
\S | Match any non whitespace character |
\d | Matching number |
\D | Match non numbers |
\b | Match the beginning or end of a word |
^ | Start of matching string |
$ | End of matching string |
sign
The last "g" flag indicates the global status used by the regular expression. Using the global flag indicates that the search operation in the searched string will find all matching items, not just the first one. This is also called global matching [Related flags include i (ignoreCase, meaning ignore case) and m (multiline, meaning cross row is allowed)], as shown in the following table
Add a sign
sign | describe |
---|
g | global search |
i | Case insensitive search |
m | Multiline Search |
y | Perform a "sticky" search, matching starts from the current position of the target string, and you can use the y flag |
qualifier
Then let's look at the main part in the middle (([^,]+,)\2+)
Character groups are []
(square brackets) list all possible matches, +
It means matching the previous expression one or more times,
Supplement a lower limit identifier
code | meaning |
---|
* | Zero reset or more times |
+ | Repeat once or more |
? | Repeat zero or one time |
{n} | Repeat n times |
{n,} | Repeat n or more times |
{n,m} | Repeat n to m times |
.replace(/(([^,]+,)\2+)/g,'[$1],')
In $1
Replace the matched part with [$1]
,
The matching part is worth more than two identical values
.replace(/,(]|$)/g,'$1')
Is to go ,]
In ,
Well written···
This article Regular matching of URLs is also used
This article is written by Chakhsu Lau Creation, adoption Knowledge Sharing Attribution 4.0 International License Agreement.
All articles on this website are original or translated by this website, except for the reprint/source. Please sign your name before reprinting.