Regular expression notes

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

definition:

/ / 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.

        ex.

<script>
    var reg=/a/;
    console.log(reg.exec("abacduab")); //0
    console.log(reg.exec("aowecad"));//5
    console.log(reg.exec("aowecad"));//null
    console.log(reg.lastIndex) 
    //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
</script>

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.

        ex. 

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

Modifier:

  • 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

ex.

<script>
    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
</script>

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.

ex.

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

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

ex.

console.log(str.search(reg));

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

ex.

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

Metacharacter:

// . : Wildcard representing any character

var str="catcotcuut";
console.log(str.match(/c..t/g));

//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

classifier:

n+

Matches any string that contains at least one n.

n*

Matches any string containing zero or more n's.

n?

Matches any string containing zero or one n.

n{X}

Matches a string containing a sequence of X n.

n{X,Y}

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

n{X,}

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

n$

Matches any string ending in n.

^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
    console.log("aaabbbccc".match(/a{2,11}b{3}c{3}/));
    console.log("aaaaaabbbccc".match(/a{2,11}b{3}c{3}/));
    console.log("aaaaaaaaaaabbbccc".match(/a{2,11}b{3}c{3}/));
    console.log("aabbbccc".match(/a{2,11}b{3}c{3}/));

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>";
        str=str.replace(/\<.*?\>/g,function(item){
        return "<"+item.slice(1,-1)+">";
    })
    console.log(str)

() 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

    console.log(str.replace(/a(?=c)/g,"z"))

//Post negative assertion

    console.log(str.replace(/a(?!c)/g,"z"))

//Pre affirmative assertion< Is the front

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

//Prepositional negative assertion

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

Repeat (\ w):

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

console.log("aaaaaaaaabbbbbbcccccd".match(/(\w)\1+/g));

Topics: regex