day_ Regular expression of 21 JS

1, Regular expression: Verification (validation) of string -- string

Regular creation

Literal create / match string/

Instantiate object new RegExp('matching string ')

/ / double slash --- judge whether there are characters in the string that can match good

        const reg1 = /good/

/ / short for regular expression

/ / instantiate the object

        const reg2 = new RegExp('good')

        console.log(reg1,reg2);

        //   /good/ /good/

2, Regular modifier

i ignore ignore case

        var reg1 = /tmd/i

g global global

        const reg2 = /tmd/g

/ / the first parameter of the constructor represents the matching character, and the second parameter represents the modifier

        const reg3 = new RegExp('tmd','gi')

        const str = 'hello tmd hei Tmd tmd'

/ / replace only replaces the first one by default

        console.log(str.replace('tmd','**'));

        console.log(str.replace(/tmd/ig,'**'));

        console.log(str.replace(reg3,'**'));

3, Regular method

text() returns a Boolean value

exec() returns an array - always matches one. If it doesn't match, it returns null

Supplementary knowledge points

            [ ]  ->  true

        const reg = /tmd/ig

        const str = 'hello tmd Tmd'

        // The test regular method tests whether there is content matching the regular in the string -- Boolean values are generally sub regular, and global matching is not required
        console.log(reg.test(str)); // true /


        // It returns an array - always matches one. If it does not match, it returns null
        console.log(reg.exec(str));
        // ['Tmd', index: 10, input: 'hello tmd Tmd', groups: undefined]

4, Regular content

Regular syntax

| or

[] or (interval) [0-9] [1-9] [a-z] [A-Z] [0-9a-zA-Z]

Spaces will also be matched - generally regular do not write spaces

Regular metacharacter

Times matching

* 0 or more times {0,}

          ? 0 or 1 times {0,1}

+ 1 or more {1,}

{m,n} m times to N times

{m,} at least m times

{m} m times

       

^ to start

$in ending

^ $can be used at the same time


 

        . Match any character

 

5, Escape character

Escape use\

        //  Match\/
        const reg = /\ \//
        console.log(reg.test(' \/'));

        //  Match
        const reg2 = /\./

        //  Match[
        const reg3 = /\[/

        console.log(reg3.test('[]]')); // true

        console.log(reg2.test('hq')); // false

        // + ? *  {} the number of matches. You need something matching in front
        // const reg4 = /+/   
        // Error reporting / + /: Nothing to repeat
        const reg4 = /\+/   
        console.log(reg4.test('+'));  // true
      

6, Abbreviation

\ d number

\ D non numeric

\ w # number, letter, underline [0-9a-zA-Z_]

\ W , non numeric, alphabetic, underline

\ s # match white space characters

\ S # match non white space characters

White space characters are different from empty characters

Empty character {0}

7, Chinese

Chinese matching

            [\u4e00-\u9fa5]

        const reg = /^[\u4e00-\u9fa5]{3,6}$/

        console.log(reg.test('How do you do')); // true

VIII. The method of using regular strings

charAt() accesses the value corresponding to the corner mark

charCodeAt() accesses the ASCII value of the value corresponding to the corner mark

        String.fromCharCode() converts ASCII values to corresponding characters

includes() determines whether a value exists in the array

indexOf() determines whether a value exists in the array and returns the subscript that appears for the first time. If it cannot be found, it will be returned

Return - 1

lastIndexOf() determines whether a value exists in the array and returns the last subscript

slice(i,i) intercepts some values in the array and returns a new array

substring(i,i) / / intercept the 2nd to 3rd corner markers of (1,2)

substr(i, quantity) / / (2,2) cut 2 from the second

toUpperCase() upper case and toLowerCase() lower case

concat() + concatenate an array and return a new array

split() cuts the string into an array

replace(old,new) (only the first one will be replaced by default)

trim() removes the leading and trailing spaces
 

Regular methods are acceptable

Replace (to find, replace) will be replaced only once by default, so regular is required

search() is generally the same as indexOf, but search can also accept regular

match() finds the characters that meet the conditions. By default, only the first one will be found. Only global matching can find all the characters. If it cannot be found, null will be returned

split() cuts strings into arrays, which can also accept regular writing

        // Replace sensitive words
        function replaceMinGan(str,arr,n){
            n = n || '**';
            // const mgc = ['tmd','md','wc'];
            const word = arr.join('|');
            const reg = new RegExp(word,'ig')
            return str.replace(reg,n)
        }

        const mgc = ['tmd','md','wc']
        const res = replaceMinGan('wc,Regular truth tmd Easy,Wc',mgc)
        console.log(res); // **, regular is so * * simple**
        const str2 = 'good day day up'
        console.log(str2.search('day'));  
        // 5     search = indexOf


        console.log(str2.search(/da*y/gi)); 
        // 5 regular search can be used, but indexOf cannot

        console.log(str2.match(/da*y/gi)); // Characters ['day ','day'] that meet the criteria were found
        const str3 = 'good   good  study  day  day  up'        
        // Greedy matching
        console.log(str3.split(/ +/));
        // ['good', 'good', 'study', 'day', 'day', 'up']

9, Common regular exercises

/ / mailbox 123@qq.com   123@qq.cn 123@qq.com.cn

/ / there must be @, @ must be preceded by content Com or cn or com.cn end

        const reg = /^[\w,\+]+@\w+\.(com|cn|com\.cn)$/

        console.log(reg.test('1,+23@q.com.cn'));
        // true

/ / ID number 18 digit 17 digit + digit or X

/ / ID card: 6 digits; 4 digits; year of birth: February 2; Date: 3 digits, or X

        // ID number 18 digit 17 digit + digit or X
        const reg2 = /^\d{17}[\dX]$/ 
        console.log(reg2.test('22222219990929191X'));
        // true

        // The ID card has 6 digits, 4 digits, year of birth, February 2, date, 3 digits, or X
        const reg3=/^\d{6}(19|20)\d{2}(0|1)\d(0|1|2|3)\d{4}[\dX]$/
        console.log(reg3.test('22222219991929191X'));
        // true

/ / delete all spaces

        // Delete all spaces
        const str = '   good good  study day day up  '
        // const arr = ['',' ','  '] ...
        // replace
        console.log(str.replace(/ +/g,''));
         // Replacing good study dayup with empty is equivalent to deleting it

/ / delete the leading and trailing spaces

        console.log(str); 
        console.log(str.replace(/^ +| +$/g,''));
        // good good  study day day up

/ / delete extra spaces

        console.log(str.replace(/ +/g,' '));
        //  good good study day day up 

 

 

 

 

Keywords: Javascript Front-end regex

Added by Hatch on Wed, 05 Jan 2022 16:19:37 +0200