Js regular test match exec detailed analysis

  • 2020-03-30 01:29:38
  • OfStack

Regular expression gi
I didn't understand it at the beginning, but I found it on the Internet. Now I will share it with you
The general term in the expression: /pattern/flags (/ pattern/flag)

The constructor function method is used as follows:
New RegExp (" pattern ", "flags" []) that new RegExp (" mode "[," tag "])
The pattern (pattern)
Text that represents a regular expression
Flags (tag)
If specified, flags can be one of the following values:
G: global match
I: ignore case
Gi: both global match and ignore case
Expressions create the same regular expressions as:

/ ab + c/gi

The differences and meanings 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)

The test, the match, the exec

Regular expressions are used a lot in JavaScript, and the two functions Match and Test are used a lot in regular expressions, as well as Exec.

Match Example

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');

The Test Example

var str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
var regexp = /[A-E]/gi;
var rs = regexp.test(str);
// rs = true; boolean

Exc Example

var str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
var regexp = /[A-E]/gi;
var rs;
while ((rs = regexp.exec(str)) != null)
    document.write("<br />");

Related articles: