SoFunction
Updated on 2025-03-04

A brief discussion on regular expressions and an example introduction

I haven't read the regular expression for a long time. I happened to use it today. I have reviewed the past and learned it. Let's learn it.
50% original in practice learning from one example.
1 Basic knowledge of javascript regular expressions
1 JavaScript regular object creation and usage
Declare javascript regular expressions
Copy the codeThe code is as follows:

var reCat = new RegExp("cat");
You can, too
var reCat = /cat/; //Perl style (recommended)

2 The most commonly used test exec match search replace split 6 methods
1) test Check whether the specified string exists
Copy the codeThe code is as follows:

var data = "123123";
var reCat = /123/gi;
alert((data)); //true
//Check whether the characters exist g Continue to go down i is case-sensitive

2) exec returns the query value
Copy the codeThe code is as follows:

var data = "123123,213,12312,312,3,Cat,cat,dsfsdfs,";
var reCat = /cat/i;
alert((data)); //Cat

3) match gets the query array
Copy the codeThe code is as follows:

var data = "123123,213,12312,312,3,Cat,cat,dsfsdfs,";
var reCat = /cat/gi;
var arrMactches = (reCat)
for (var i=0;i < ; i++)
{
alert(arrMactches[i]); //Cat cat
}

4) search Return to search location Similar to indexof
Copy the codeThe code is as follows:

var data = "123123,213,12312,312,3,Cat,cat,dsfsdfs,";
var reCat = /cat/gi;
alert((reCat)); //23

5) replace replace characters Use regular replacement
Copy the codeThe code is as follows:

var data = "123123,213,12312,312,3,Cat,cat,dsfsdfs,";
var reCat = /cat/gi;
alert((reCat,"libinqq"));

6) split using regular segmentation array
Copy the codeThe code is as follows:

var data = "123123,213,12312,312,3,Cat,cat,dsfsdfs,";
var reCat = /\,/;
var arrdata = (reCat);
for (var i = 0; i < ; i++)
{
alert(arrdata[i]);
}

3 Learn Simple Class Negative Class Scope Class Combination Class
Copy the codeThe code is as follows:

//Simple class
var data = "1libinqq,2libinqq,3libinqq,4libinqq";
var reCat = /[123]libinqq/gi;
var arrdata = (reCat);
for (var i = 0; i < ; i++)
{
alert(arrdata[i]); // 1libinqq 2libinqq 3libinqq
}
//Negative class
var data = "alibinqq,1libinqq,2libinqq,3libinqq,4libinqq"; //\u0062cf
var reCat = /[^a123]libinqq/gi;
var arrdata = (reCat);
for (var i = 0; i < ; i++)
{
alert(arrdata[i]); //4libinqq
}
//Scope Class
var data = "libinqq1,libinqq2,libinqq3,libinqq4,libinqq5"; //\u0062cf
var reCat = /libinqq[2-3]/gi;
var arrdata = (reCat);
for (var i = 0; i < ; i++)
{
alert(arrdata[i]); // libinqq2 libinqq3
}
//Combination Class
var data = "a,b,c,w,1,2,3,5"; //\u0062cf
var reCat = /[a-q1-4\n]/gi;
var arrdata = (reCat);
for (var i = 0; i < ; i++)
{
alert(arrdata[i]); // a b c 1 2 3
}

These are the most basic methods of using js regularity. If you don’t know what to do, please copy it to your notebook to practice. If you read it, then read it below.
2. JavaScript regular expressions are grouping knowledge
1) Simple grouping
Copy the codeThe code is as follows:

<script language="JavaScript">
<!--
/* Regular expressions Simple grouping
For example, we want to find the string MouseMouse
var reCat = /MouseMouse/gi;
Although this is OK, it is a bit of a waste. What should I do if I don't know how many times Mouse appears in a string, what if I repeat it multiple times.
var reCat = /(mouse){2}/gi; The meaning column of brackets Mouse will appear twice in a row.
*/
var data = "Ah-mousemouse";
var reCat = /(mouse){2}/gi;
var arrdata = (reCat);
for (var i = 0; i < ; i++)
{
alert(arrdata[i]);
}
//-->
</script>

2 Complex grouping
Copy the codeThe code is as follows:

<script language="JavaScript">
<!--
/* Regular expressions Complex grouping
? Zero or once
* Zero or multiple times
+ At least once or multiple times
*/
var data = "bb ba da bad dad aa ";
var reCat = /([bd]ad?)/gi; // Match ba da bad dad
var arrdata = (reCat);
for (var i = 0; i < ; i++)
{
alert(arrdata[i]);
}
// At the same time, don't mind putting the group in the middle of the group
// var re = /(mom( and dad)?)/; match mom or mon and daa
//-->
</script>

3 Backreferences
Copy the codeThe code is as follows:

<script language="JavaScript">
<!--
/*Regular expression Backreference*/
var sToMatch = "#123456789";
var reNumbers = /#(\d+)/;
(sToMatch);
alert(RegExp.$1);
/*
This example tries to match the pounds followed by several or more numbers and group the numbers
to store them. After calling the test method, all backreferences are saved to the RegExp constructor
Starting with RegExp.$1 (which saves the first backreference), if there is a second backreference, it is
RegExp.$2, if there is a third backreference, it is RegExp.$3, and so on. Because of this group
It matches "123456780", so this string is stored in RegExp.$1.
*/
var sToChange = "1234 5678";
var reMatch = /(\d{4}) (\d{4})/;
var sNew = (reMatch,"$2 $1");
alert(sNew);
/*
In this example, the regular expression has two groups, each group has four numbers. exist replace() The second parameter of the method
In , $2 is equivalent to "5678" and $1 is equivalent to "1234", corresponding to their occurrence order in the expression.
*/
//-->
</script>

4 Candidates
Copy the codeThe code is as follows:

<script language="JavaScript">
<!--
/*regular expression candidate */
var sToMatch1 = "red";
var sToMatch2 = "black";
var reRed = /red/;
var reBlack = /black/;
alert((sToMatch1) || (sToMatch1));
alert((sToMatch2) || (sToMatch2));
/*
Although this can complete the task, it is very long and there is another way to do it, which is the candidate operator of regular expressions.
*/
var sToMatch1 = "red";
var sToMatch2 = "black";
var reRedOrBlack = /(red|black)/;
alert((sToMatch1));
alert((sToMatch2));
//-->
</script>

5 Non-capturing grouping
Copy the codeThe code is as follows:

<script language="JavaScript">
<!--
/* Regular expressions Non-capturing grouping
If you want to create a non-capturing group, just add a question mark and a colon immediately following the opening bracket:
*/
var sToMatch = "#123456789";
var reNumbers = /#(?:\d+)/;
(sToMatch);
alert(RegExp.$1);
/*
The last line of code in this example outputs an empty string because the group is non-capture.
*/
var sToMatch = "#123456789";
var reNumbers = /#(?:\d+)/;
alert((reNumbers,"abcd$1"));
/*
Because of this, the replace() method cannot use any backreferences through the RegExp.$x variable, this code
The output "abcd$1" is not abcd123456789, because $1 is not considered a backreference here.
*/
//-->
</script>

6 Preview
Copy the codeThe code is as follows:

<script language="JavaScript">
<!--
/* Regular expressions Lookalike
Lookahead Just like its name, it tells the regex operator to look forward to some characters instead of moving positions
*/
var sToMatch1 = "bedroom";
var sToMatch2 = "bedding";
var reBed = /bed(?=room)/;
alert((sToMatch1)); //true
alert((sToMatch2)); //false
//Negative forward
var sToMatch1 = "bedroom";
var sToMatch2 = "bedding";
var reBed = /bed(?!room)/;
alert((sToMatch1)); //false
alert((sToMatch2)); //true
//-->
</script>

7 Boundaries
Copy the codeThe code is as follows:

<script language="JavaScript">
<!--
/* Regular expression boundary
^ The beginning of the line
$ line end
\b The boundary of the word
\B Non-word boundary
*/
var sToMatch = "Important word is the last one.";
var reLastWord = /(\w+)\.$/;
(sToMatch);
alert(RegExp.$1); //one
/*
If you want to find a word, but want it to appear only at the end of the line, you can use the dollar sign ($) to represent it:
*/
var sToMatch = "Important word is the last one.";
var reLastWord = /^(\w+)/;
(sToMatch);
alert(RegExp.$1); //Important
/*
In this example, the regular expression looks for one or more word characters after the starting position of the line. If non-word characters are encountered
The match stops, returning to Important. This example can also be implemented with word boundaries.
*/
var sToMatch = "Important word is the last one.";
var reLastWord = /^(.+?)\b/;
(sToMatch);
alert(RegExp.$1); //Important
/*
Here, regular expressions are made using lazy quantifiers to formulate any character that can appear before the word boundary, and can appear once or
Multiple times (if greedy quantifiers are used, the expression matches the entire string).
*/
var data = " First second thind fourth fifth sixth ";
var reCat = /\b(\S+?)\b/g;
var arrdata = (reCat);
for (var i = 0; i < ; i++)
{
alert(arrdata[i]);
}
/*
Use word boundaries to easily extract words from strings.
*/
//-->
</script>

8 Multi-line mode
Code
Copy the codeThe code is as follows:

<script language="JavaScript">
<!--
/* Regular expression Multi-line mode
To create a multi-line pattern, just one word at the end of the line the regular expression wants to match
*/
var data = " First second\n thind fourth\n fifth sixth";
var reCat = /(\w+)$/g;
var arrdata = (reCat);
for (var i = 0; i < ; i++)
{
alert(arrdata[i]);
}
/*
Only one word sixth is returned above, because the newline character blocks the match and can only match one word at the end of the line.
Of course, you can also use the split() method to split the string into an array, but you have to match each line separately.
In the past, I was often half-hearted when I didn't read books well, but I still read halfway, which led to a lot of splits. In fact, it's very simple as below
Example Only the m parameter is required for multiple row matching.
*/
var data = " First second\n thind fourth\n fifth sixth";
var reCat = /(\w+)$/gm;
var arrdata = (reCat);
for (var i = 0; i < ; i++)
{
alert(arrdata[i]);
}
//-->
</script>

At this end, these are the basic methods of javascript regular expressions. If you can read complex regular expressions, you will feel enlightened.