Home >Web Front-end >JS Tutorial >Detailed analysis of regular test, match, and exec of js_javascript skills

Detailed analysis of regular test, match, and exec of js_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:01:411358browse

Regular expression gi
I couldn’t understand it at first. I found it on the Internet and saw it. Now I share it with you
The common term of the current expression: /pattern/flags i.e. (/pattern/mark)

The constructor function method is used as follows:
new RegExp("pattern"[, "flags"]) i.e. new RegExp("pattern"[,"flag"])
Parameters:
pattern(pattern)
Text representing the regular expression
flags(mark)
if When specifying this item, flags can be one of the following values:
g: global match (complete match)
i: ignore case (ignore case)
gi: both global match and ignore case (match all Possible values, also ignoring case)
expression creates the same regular expression. For example:

/ab c/gi

The difference and meaning of /i,/g,/ig,/gi,/m in regular expressions

/i (ignore case)
/g (Full-text search for all matching characters)
/m (Multi-line search)
/gi (Full-text search, ignore case)
/ig(Full text search, ignore case)

test,match,exec

Regular expressions are often used in JavaScript, and the two functions Match and Test are often used in regular expressions, and of course Exec. Here are code examples to distinguish the differences between them.

Match Example

Copy code The code is as follows:

var str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
var regexp = /[A-E]/gi;

var rs = str.match(regexp);

//rs= Array('A', 'B', 'C', 'D', 'E', 'a', 'b', 'c', 'd', 'e');


Test Example
Copy code The code is as follows:

var str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
var regexp = /[A-E]/gi;

var rs = regexp.test(str);

// rs = true; boolean


Exc Example
Copy code The code is as follows:

var str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
var regexp = /[A-E]/gi;
var rs;
while ((rs = regexp.exec(str)) != null)
{
document.write(rs);
document.write(regexp.lastIndex);
document.write("
");
}
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn