JavaScript RegExp object

What is RegExp?

Regular expression describes the pattern object of characters.

When you retrieve some text, you can use a pattern to describe what you want to retrieve. RegExp is this pattern.

Simple pattern can be a single character.

More complex patterns include more characters and can be used for parsing, format checking, replacement, etc.

You can specify the search position in the string, the type of characters to be searched, etc.

JavaScript provides a RegExp object to complete operations and functions related to regular expressions. Each regular expression pattern corresponds to a RegExp instance


There are two ways to create instances of RegExp objects.
Use the explicit constructor of RegExp, the syntax is: new RegExp("pattern"[,"flags"]).
Use the implicit constructor of RegExp in plain text format: /pattern/[flags].

The pattern part is the regular expression pattern text to be used, which is required. In the first method, the pattern part exists in the form of a JavaScript string and needs to be enclosed in double quotes or single quotes; in the second method, the pattern part is nested between two "/" and quotation marks cannot be used. . The

flags part sets the flag information of the regular expression, which is optional. If the flags part is set, in the first way, it exists in the form of a string; in the second way, it is in the form of text immediately after the last "/" character. flags can be a combination of the following flag characters.
g is a global flag. If this flag is set, a search and replace operation on a text will affect all matching parts of the text. If this flag is not set, only the earliest match is searched and replaced.

<!DOCTYPE html>
<html>
<head> 
<meta charset="utf-8"> 
<title>php中文网(php.cn)</title> 
<script type="text/javascript">
var myString="这是第一个正则表达式的例子"; 
var myregex = new RegExp("一个"); // 创建正则表达式 
if (myregex.test(myString)){ 
document.write("找到了指定的模式!"); 
} 
else{ 
document.write("未找到指定的模式。"); 
} 
</script>
</head>
<body>
</body>
</html>

RegExp modifier

g is a global flag. If this flag is set, a search and replace operation on a text will affect all matching parts of the text. If this flag is not set, only the earliest match is searched and replaced.
i is a case-ignoring flag. If this flag is set, case will be ignored when doing match comparisons.
m is a multi-line flag. If this flag is not set, the metacharacter "^" will only match the beginning of the entire searched string, and the metacharacter "$" will only match the end of the searched string. If this flag is set, "^" can also match the position after "\n" or "\r" in the searched string (i.e., the beginning of the next line), and "$" can also match the position after "\n" or "\r" in the searched string. Matches the position after "\n" or "\r" in the string (that is, the end of the next line).

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>php中文网(php.cn)</title>
</head>
<body>
<script>
var str="Is this all there is?";
var patt1=/is/gi;
document.write(str.match(patt1));
</script>
</body>
</html>


RegExp object properties

RegExp object properties are divided into static properties and instance properties.

1 Static attributes
(1) index attribute. Is the starting position of the first matching content of the current expression pattern, counting from 0. Its initial value is -1, and every time a successful match occurs, the index attribute will change accordingly.
(2)Input attribute. Returns the current string being acted on, which can be abbreviated as $_, and the initial value is the empty string "".
(3)lastIndex attribute. It is the next position of the last character in the content that the current expression pattern first matches. Counting starts from 0. It is often used as the starting position when continuing the search. The initial value is -1, which means that the search starts from the starting position, and every time it succeeds When matching, the lastIndex attribute value will change accordingly.
(4)lastMatch attribute. Is the last matching string of the current expression pattern, which can be abbreviated as $&. Its initial value is the empty string "". The value of the lastMatch attribute changes with each successful match.
(5)lastParen attribute. If there is an enclosed submatch in the expression pattern, it is the substring matched by the last submatch in the current expression pattern, which can be abbreviated as $+. Its initial value is the empty string "". The value of the lastParen attribute changes with each successful match.
(6) leftContext attribute. It is everything to the left of the last matching string in the current expression pattern, which can be abbreviated as $` (where "'" is the backquote mark under "Esc" on the keyboard). The initial value is the empty string "". Each time there is a successful match, its property value changes.
(7)rightContext attribute. It is everything on the right side of the last matching string in the current expression pattern, which can be abbreviated as $'. The initial value is the empty string "". Each time there is a successful match, its property value changes.
(8)$1…$9 attributes. These properties are read-only. If there are enclosed submatches in the expression pattern, the $1...$9 attribute values ​​are the contents captured by the first to ninth submatches respectively. If there are more than 9 submatches, the $1...$9 attributes correspond to the last 9 submatches respectively. In an expression pattern, you can specify any number of parenthesized submatches, but the RegExp object can only store the results of the last nine submatches. In the result array returned by some methods of the RegExp instance object, all submatch results within parentheses can be obtained.

2 Instance attributes
(1) global attribute. Returns the status of the global flag (g) specified when creating the RegExp object instance. If the g flag is set when creating a RegExp object instance, this property returns True, otherwise it returns False. The default value is False.
(2)ignoreCase attribute. Returns the status of the ignoreCase flag (i) specified when creating the RegExp object instance. If the i flag is set when creating a RegExp object instance, this property returns True, otherwise it returns False. The default value is False.
(3)multiLine attribute. Returns the status of the multiLine flag (m) specified when creating the RegExp object instance. If the m flag is set when creating a RegExp object instance, this property returns True, otherwise it returns False. The default value is False.
(4) source attribute. Returns the expression text string specified when creating the RegExp object instance.


Methods of RegExp object

1 test method
The syntax format is test(str). This method checks whether the expression pattern specified when creating a RegExp object instance exists in a string, and returns True if it exists, otherwise it returns False. If a match is found, the relevant static properties in the RegExp object are updated to reflect the match.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>php中文网(php.cn)</title>
</head>
<body>
<script>
var str = 'php';
var patt1 = new RegExp('\w', 'g'); // 有转义作为正则表达式处理
var patt2 = new RegExp('\w', 'g');  // 无转义作为字符串处理
var patt3 =/\w+/g;  // 与 patt1 效果相同
document.write(patt1.test(str)) //输出 true
document.write("<br>") 
document.write(patt2.test(str)) //输出 false
document.write("<br>") 
document.write(patt3.test(str)) //输出 true
</script>
</body>
</html>

2 exec method
The syntax format is exec(str). This method searches a string using the expression pattern specified when creating the RegExp object instance and returns an array containing the search results.
If the global flag (g) is set for the regular expression, you can continuously search in the string by calling the exec and test methods multiple times, each time starting the search for characters from the position specified by the lastIndex attribute value of the RegExp object. string.
If the global flag (g) is not set, the exec and test methods ignore the lastIndex attribute value of the RegExp object and start searching from the beginning of the string.
If the exec method does not find a match, the return value is null; if a match is found, an array is returned, and the relevant static properties in the RegExp object are updated to reflect the match. Element 0 in the returned array contains the complete matching result, while elements 1 to n are the results of each submatch defined in the expression pattern.
The array returned by the exec method has three attributes, namely input, index and lastIndex.
The input attribute is the entire searched string.
The index attribute refers to the matching position in the entire searched string.
The lastIndex attribute refers to the character position next to the last character of the matched substring.

<!DOCTYPE html>
<html>
<head> 
<meta charset="utf-8"> 
<title>php中文网(php.cn)</title> 
<script type="text/javascript">
var myString="aaa 111 bbb 222 ccc 1111 222ddd"; 
var regex = /111/; //创建正则表达式对象 
var array=regex.exec(myString); 
if (array){ 
var str="找到了匹配子串!"+"\n返回数组的值为:"+array+"\n数组元素个数:" 
+array.length+"\n被搜索的字符串为:"+array.input 
+"\n匹配子串的开始位置为:"+array.index 
+"\n匹配子串后面第一个字符的位置为:"+regex.lastIndex; 
alert(str); 
} 
else{ 
alert("未找到匹配子串!!"); 
} 
</script>
</head>
<body>
</body>
</html>


Continuing Learning
||
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> <script type="text/javascript"> var strSrc = "xxa1b01c001yya2b02c002zz"; var re = /a(\d)b(\d{2})c(\d{3})/gi; var arr, count = 0; while ((arr = re.exec(strSrc))!= null) { displayResult(); } function displayResult() { document.write("<p>这是用正则表达式/" + re.source + "/gi对字符串<br>" + RegExp.input + "进行第" + (++count) + "次搜索的结果:<br>"); document.write("RegExp.index为" + RegExp.index + "<br>"); document.write("RegExp.lastIndex为" + RegExp.lastIndex + "<br>"); document.write("RegExp.lastMatch为" + RegExp.lastMatch + "<br>"); document.write("RegExp.lastParen为" + RegExp.lastParen + "<br>"); document.write("RegExp.leftContext为" + RegExp.leftContext + "<br>"); document.write("RegExp.rightContext为" + RegExp.rightContext + "<br>"); document.write("RegExp.$1为" + RegExp.$1 + "<br>"); document.write("RegExp.$2为" + RegExp.$2 + "<br>"); document.write("RegExp.$3为" + RegExp.$3 + "<br>"); document.write("RegExp.$4为" + RegExp.$4 + "<br>"); document.write("arr.index为" + arr.index + "<br>"); document.write("arr.input为" + arr.input + "<br>"); document.write("arr.lastIndex为" + arr.lastIndex + "<br>"); document.write("返回数组的元素个数为" + arr.length + "<br>"); document.write("返回数组的内容为[]"); for(var i=0; i<arr.length; i++) { if(i < arr.length-1) document.write("" + arr[i] + ""); else document.write(("" + arr[i] + "")+"<p>"); } } </script> </head> <body> </body> </html>
submitReset Code