javaScript use regular expressions (script form)
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
test()
For detecting whether a character string matching a pattern, if the string includes text matching Returns true, otherwise returns false.
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
- Execution
exec()
matching the results will return an array.
- The first element is the source string, after the element number corresponding to the first regular parentheses.
- 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