Regular expression notes

Posted by PHP Novice on Sun, 20 Feb 2022 11:21:19 +0100


/ / regular expression is a micro language, which is the matching condition for strings

/ / literal expression

/ / var reg=new RegExp("matching regular content", "modifier");

/ / fill in the regular expression in the double slash

Regular object method:

exec(): method retrieves the specified value in the string. The return value is the value found. If no match is found, null is returned.


    var reg=/a/;
    console.log(reg.exec("abacduab")); //0
    //Here is the record of the last index recorded since the regular expression was used,
    //The next time you search, you will find the main cause of the record according to the index. Use the modifier g

text(): returns a Boolean value. The method is used to match the string. If the matching is successful, it returns true and if it fails, it returns false.


    var reg=/a/;
    console.log(reg.test("abcedf"));  // console.log() result: true.


  • i) case insensitive (performs case insensitive matching.)
  • g) global match (find all matches instead of stopping after finding the first match)
  • m) perform multi line matching.

Square brackets []: square brackets are used to find characters in a range

  • [abc]: find any character between square brackets
  • [^ abc]: find any character that is not between square brackets (antisense)
  • [0-9]: find any number from 0 to 9
  • [a-z]: find any character from small a to small z
  • [A-Z]: find any character from uppercase A to uppercase Z

String method:

replace(): used for replacement. It accepts two parameters. The first is a match, and the second can be a string or a function


    var str = "abacad";
    str = str.replace(/a/g, "z");  // console. The result of log() is zbzczd
    var i = 0;
    str = str.replace(/a/g, function(item, index) {
    return ++i;
    console.log(str);     //console. The result of log() is 1b2c3d

match(): accept a parameter to match the string regularly. If the match is successful, the array of successful matches will be returned. If the match is unsuccessful, null will be returned.


    var str = "abc345hh67";
    console.log(str.match(/\d{2}/)); //console. The result of log() is an indexed array, and the search result is 34

search(): the parameter is the same as match. It returns the index of the first matching item in the string. If there is no matching item, it returns - 1



split(): divides a string into an array of strings.


    console.log("abcdef".split(/[bd]/));  //console. The printout result of log() is ['a ',' C ',' EF ']


// . : Wildcard representing any character

var str="catcotcuut";

//Any metacharacter can be escaped to the corresponding character by using \ before it

ex.:  \.  \\   \/   \[   \{    \*    \^  \?

//\ w: letters, numbers, and underscores ([a-zA-Z0-9_])

//\ W: find non word characters ([^ a-zA-Z0-9_])

//\ d: represents a number ([0-9])

//\ D: non numeric ([^ 0-9])

//\ s: find white space characters

//\ S: find non white space characters



Matches any string that contains at least one n.


Matches any string containing zero or more n's.


Matches any string containing zero or one n.


Matches a string containing a sequence of X n.


A string that matches a sequence of X or Y n's.


Matches a string containing a sequence of at least X n.


Matches any string ending in n.


Matches any string that starts with n.

^The starting character is different from [^], ^ n here means that n is the starting character (the first character written in the regular expression is the starting character)

$terminator n $means n is the Terminator (the terminator written at the end of the regular expression)

Greedy matching:

// Greedy matching can be obtained as long as the conditions are met within the specified range

Non greedy matching:

 // .*?   Non greedy matching first uses wildcards to increase the optional range,? Indicates the first one found? Following characters
    var str="Four Chinese Classics<Journey to the West>,<Romance of the Three Kingdoms>,<Water Margin>,<The Dream of Red Mansion>";
        return "<"+item.slice(1,-1)+">";

() group:

1. The group has the function of combining the contents of multiple expressions for repetition

2. The group can extract part of the content separately

/ / when using match, if you do not use g, use () to select the metacharacters that need to be matched separately. At this time, these groups will start from subscript 1 in the array returned by match

Assertion: (var str="abacad";)

//Post affirmative assertion= It's post


//Post negative assertion


//Pre affirmative assertion< Is the front

    console.log(str.replace(/(?<=c)b/g, "z"));

//Prepositional negative assertion


Repeat (\ w):

(\ w) duplicate content to be found \ 1 repeat + repeat at least 1 time * no repetition is allowed


Topics: regex