Regular expression (continuous update)

Keywords: Google


[]: match one of them. [abc]: match a

[]: a range. [3-7]: match any number between 3 and 7, including 3 and 7

[0-9] = = [\ D] equivalent, matching number

[a-zA-Z0-9]: match case letters and numbers.

[^ a-z0-9]: matches any character not in parentheses -- excluding lowercase letters and numbers

2. Rules of combination

\d: Match the number 0-9.

\D: Match non numeric.


\w: Match alphanumeric and underline.

\W: Matches non numeric letters and underscores.

3. \s: matches white space characters, spaces, tabs, and line breaks.

^: put it in brackets to indicate negation (non negation). If it is placed at the beginning of the regular, it indicates the starting position

$: end of line match

var reg=/^hello$/;//The complete match is no longer included.
        var str='hello';
//Remove the space before and after the string -- / ^ \ s+|\s$/g
        var str='          dafsadf fdasf asdf       ';
        var reg=/^\s+|\s+$/g;

4. Number of matches

X? Matches 0 or 1 x

x * matches 0 or any number of x's

x + matches at least one x

5. Parentheses ()

5.1: as a whole

var reg=/^(xyz)+$/g;
        var str='xyzxyzxyz';

5.2: grouping: matching results and output grouping values

var reg=/(a)(b)(c)/;
        var str='abc';
var reg = /(a)(b)(c)/g;
        var str = 'abc';
        alert(str.match(reg)); //Results of abc matching
var reg = /^(\w)(\w)(\w){1,}\1\2$/g;
        var str = 'w3cfffffffw3';


x{m,n} matches at least m and at most n x -- ranges

x{m} matches m -- equivalence relation

x{m,} matches at least M. - scope

  var reg=/^(google){3,}$/;
        var str='googlegooglegooglegooglegooglegoogle';
//Using regular de duplication and counting the number of characters
        var str = 'sdfkewroiudsafjkwqruoasfdafklgkldfuoiret';
        str = str.split('').sort().join('');
        var reg = /(\w)\1*/g; //Match one or more duplicate characters.
        var str1 = '';
        str.replace(reg, function(result, value1, index, string) {
            //Result: matching result, array.
            //value1: grouped value. From the second parameter, there are several groups, there are several parameters.
            //The last two parameters are the start index position and the string itself
            //Index: start index location.
            //String: string itself
            console.log(value1 + ':' + result.length); //Statistical number
            str1 += value1;
        console.log(str1); //adefgijkloqrstuw

7. this|where|logo matches any one of this or where or logo

  var reg=/^this(a|b)this$/;
        var str='thisbthis';
        alert(reg.test(str)); //true


Posted by Kower on Fri, 10 Jan 2020 08:38:48 -0800