javaScript using regular expressions -1

javaScript use regular expressions (script form)

  • Modifiers
Modifiers description
i Perform case-insensitive match. (Case-sensitive match)
g Perform a global match (find all matches rather than stopping after the first match).
m The implementation of multi-line matching.
  • Related Methods
    1. test() For detecting whether a character string matching a pattern, if the string includes text matching Returns true, otherwise returns false.
    2. exec()Regular expression for matching a search string.
  • Simple match
var str = 'www.baidu.com';
var reg = /baidu/;
print(reg.test(str));//true
var make = reg.exec(str);
print(make);//baidu
  • Matching more results
    1. Execution exec()matching the results will return an array.
    2. The first element is the source string, after the element number corresponding to the first regular parentheses.
    3. Executed once exce(), if not specified in the regular end of the match to the end of a line ( \n| \r), To match the second line you need to perform aexce()
    var str = 'www.baidu.com';
    //            1     2      3
    var reg = /(\w+)\.(\w+)\.(\w+)/ig;
    var make = reg.exec(str);
    print(make);//www.baidu.com,www,baidu,com
    var str = 'www.baidu.com\rwww.google.com';
    var reg = /(\w+)\.(\w+)\.(\w+)/ig;
    var make = reg.exec(str);
    print(make);//www.baidu.com,www,baidu,com
    make = reg.exec(str);
    print(make);//www.baidu.com,www,baidu,com

Guess you like

Origin www.cnblogs.com/lingdu9527/p/11858465.html
Recommended