Home  >  Article  >  Web Front-end  >  JavaScript study notes (12) RegExp type introduction_basic knowledge

JavaScript study notes (12) RegExp type introduction_basic knowledge

WBOY
WBOYOriginal
2016-05-16 17:52:36957browse

1. Matching pattern tag:
g ------- represents the global pattern, instead of ending immediately when the first successful match is found
i ------- represents the case-insensitive pattern
m ----- means multi-line matching. When one line ends, continue matching to the next line

Copy code The code is as follows:

//Match the first bat or cat, case-insensitive
var pattern1 = /[bc]at/i
var pattern2 = new RegExp("[bc]at" ,"i"); //Same as above

Use RegEXp to convert the literal mode into a string
/[bc]at/ =======> "\[ bc\]at"
/.at/ ==========> "\.at"
/name/age/ ======> "name\/age "
/d.d{1,2}/ ======> "\d.\d{1,2}"
/w\hello\123/ ====>"\ w\\hello\\123

2.RegExp instance attribute
global - Boolean value, indicating whether the g flag is set
ignoreCase - Boolean value, indicating whether the i flag is set
multiline - Boolean value, indicating whether the m flag is set
lastIndex - integer, indicating the position to start searching for the next match
source - string in literal form
Copy code The code is as follows:

var pattern2 = new RegExp("\[bc\]at","i");
alert(pattern2.source); //"[bc]at"

3.RegExp instance method
exec() method accepts one parameter, which is the character to which the regular expression is applied String,
returns an array and two attributes index and input. The first item in the array is the string matching the entire pattern, and the other items are strings matching the capturing groups in the pattern
Copy code The code is as follows:

var text "mom and dat and baby";
var pattern = /mom( and dad ( and baby)?)?/gi;

var matches = pattern.exec(text);
alert(matches.index); //0
alert(matches.input); / /"mom and dad and baby"
alert(matches[0]); //"mom and dad and baby"
alert(matches[1]); //" and dad and baby"
alert(matches[2]); //" and baby"

Every time exec() is called in non-global mode, the first matched item is returned.
Every time in global mode Calling ecec() every time returns the next match of the string, and the value of lastIndex will be increased

The test() method accepts one parameter, which is the string to which the regular expression is applied. If it matches, it returns true, otherwise it returns false
Copy code The code is as follows:

var text = "000-00-0000";
var pattern = /d{3}-d{2}-d{4}/;

if (pattern.test(test)) {
alert("Match! ");
}

4.RegExp constructor attribute
Copy code The code is as follows :

var text = "this has been a short summer";
var pattern = /(.)host/g;

if (pattern.test(text) ){
alert(RegExp.input); //this has been a short summer The latest string to be matched, equivalent to RegExp.$_, opera does not support
alert(RegExp.lastMatch); / /short The most recent match, equivalent to RegExp["$&"], opera does not support
alert(RegExp.lastParen); //s The most recent matching capture group, equivalent to RegExp["$ " ] ,opera does not support
alert(RegExp.leftContext); //this has been a The text before the match, equivalent to RegExp["$`"]
alert(RegExp.rightContext); // summer The text after the matching item is equivalent to RegExp["$'"],
alert(RegExp.multiline); //false Is it multi-line mode, equivalent to RegExp["$*"], opera and IE None support
}

RegExp.$1 …… RegExp.$9 storage capture group
Copy code The code is as follows:

var text = "this has been a short summer";
var pattern =/(..)or(.)/g;
if ( pattern.test(text)) {
alert(RegExp.$1); //sh
alert(RegExp.$2); //t
}
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