g stands for global match
m means that multiple lines can be matched
i stands for case-insensitive matching
^ matches the beginning of the input string
$ matches the end of the input string
* Matches the preceding subexpression zero or more times. Equivalent to {0,}
+ Matches the preceding subexpression one or more times. Equivalent to {1,}
? Matches the preceding subexpression zero or once. Equivalent to [0,1} when this character is followed by any other qualifier (*, +, ?, {n}, {n,}, {n, m}), the matching pattern is non-greedy. Non-greedy mode matches as little of the searched string as possible, while the default greedy mode matches as much of the searched string as possible. For example, for the string "oooo", 'o+?' will match a single "o", while 'o+' will match all 'o's.
d matches a numeric character. Equivalent to [0-9]
D matches a non-numeric character. Equivalent to [^0-9]
w , equivalent to "[A-Za-z0-9_]"
W matches any non-word character, equivalent to "[^A-Za-z0-9]"
s matches any whitespace character, including spaces, tabs, form feeds, etc. Equivalent to [fnrtv]
S matches any non-whitespace character. Equivalent to [^frntv]
b matches a word boundary, which is the position between a word and a space.
B matches non-word boundaries.
(pattern) matches pattern and gets the match. The matches obtained can be obtained from the generated Matches collection, using the SubMatches collection in VBScript or the $0…$9 properties in JScript.
(?:pattern) matches pattern but does not obtain the matching result, which means that this is a non-acquisition match and is not stored for later use. This is useful when using the "or" character (|) to combine parts of a pattern. For example, 'industr(?:y|ies) is a shorter expression than 'industry|industries'.
(?=pattern) forward lookup, matching the search string at the beginning of any string matching pattern. This is a non-fetch match, that is, the match does not need to be fetched for later use. For example, 'Windows (?=95|98|NT|2000)' matches "Windows" in "Windows 2000" but not "Windows" in "Windows 3.1". Prefetching does not consume characters, that is, after a match occurs, the search for the next match begins immediately after the last match, rather than starting after the character containing the prefetch.
(?!pattern) Negative lookahead matches the search string at the beginning of any string that does not match pattern. Negative lookahead matches the search string at any point where a string not matching pattern. This is a non-fetch match, that is, the match does not need to be fetched for later use. For example, 'Windows (?!95|98|NT|2000)' can match "Windows" in "Windows 3.1", but not "Windows" in "Windows 2000". Pre-checking does not consume characters, that is, after a match occurs, the search for the next matching starts immediately after the last match, instead of starting
to match 2-4 Chinese character
program codes
after the characters containing the pre-check.
/^[u4e00-u9fa5]{2,4}$/g;
Matches 6 to 18 (letters, numbers, underscores) character
program codes
/^w{6,18}$/;
program code
/^[A-Za-z0-9_]$/;
Match HTML tag
program code
/<[^>]*>|</[^>]*>/gm;
program code
/</?[^>]+>/gm;
Match the left and right spaces
program code
/(^s*)|(s*$)/g;
Priority order (from high to low)
escape character
(),(?:),(?=),[] round brackets and square brackets
* , + , ? , {n} , {n,} , {n,m} qualifier
^ , [vapour:content]nbsp; position and order
| The "or" operation
matches two consecutive identical word
program codes
separated by spaces
/b([az]+) 1b/gim ;
In this example, the subexpression is each term between the parentheses.
The captured expression consists of one or more alphabetic characters, as specified by '[az]+'.
The second part of the regular expression is a reference to the previously captured submatch, which is the second occurrence of the word matched by the appended expression.
'1' is used to specify the first submatch. Word boundary metacharacters ensure that only individual words are detected.
If this were not done, phrases such as "is issued" or "this is" would be incorrectly recognized by the expression.
program code
var ss = "Is is the cost of gasoline going up up?. Is is the cost of gasoline going up up?.";
var re = /b([az]+) 1b/gim;
var rv = ss.replace(re,"$1");
document.write(rv) //Output "Is the cost of gasoline going up?. Is the cost of gasoline going up?. "
program code
/bCha/
matches the first three characters of the word 'Chapter' because they appear after a word boundary
program code
/terb/
matches 'ter' in the word 'Chapter' because it appears before a word boundary
program code
/Bapt/
matches 'apt' because it is in the middle of 'Chapter', but does not match 'apt' in 'aptitude' because it is after a word boundary
*/
Match URL address
program code
/(w+)://([^/:]+)(:d*)?([^#]*)/
Decompose the following URI into protocols (ftp, http, etc), Domain name address and page/path:
http://msdn.microsoft.com:80/scripting/default.htm
The first additional subexpression is used to capture the protocol portion of the web address. This subexpression matches any word preceded by a colon and two forward slashes. The second additional subexpression captures the domain address of the address. This subexpression matches any character sequence that does not include the '^', '/', or ':' characters. The third additional subexpression captures the website port number, if one is specified. This subexpression matches zero or more numbers followed by a colon. Finally, the fourth additional subexpression captures the path and/or page information specified by the web address. This subexpression matches one or more characters except '#' or spaces.
When the regular expression is applied to the URI shown above, the submatches include the following:
RegExp.$1 contains "http"
RegExp.$2 contains "msdn.microsoft.com"
RegExp.$3 contains ":80"
RegExp.$4
Method1
thatcontains the "/scripting/default.htm"
regular expression.
The test methodreturns a Boolean value, which indicates whether the pattern exists in the string being searched.
rgExp.test(str)
The properties of the global RegExp object are not modified by the test method.
example1
http://www.knowsky.com/
program code
var url=" http://msdn.microsoft.com:80/scripting/default.html ";
var reg=/(w+)://([^/:]+)(:d*)?([^#]*)/;
var flag=reg.test(url);
flag //return true
RegExp.$1 //Return "http"
RegExp.$2 //Return "msdn.microsoft.com"
RegExp.$3 //Return ":80"
$egExp.$4 //Return "/scripting/default.html"
Neither the search nor test methods can update the global RegExp object, so RegExp.input, RegExp.index, and RegExp.lastIndex return undefined
2. The match method
uses a regular expression pattern to perform a search on the string and returns the result containing the search as an array.
program code
stringObj.match(rgExp)
If the match method does not find a match, it returns null. If a match is found an array is returned and the properties of the global RegExp object are updated to reflect the match.
The array returned by the match method has three attributes: input, index and lastIndex.
The Input property contains the entire string being searched for.
The Index property contains the position of the matching substring within the entire search string.
The LastIndex property contains the position next to the last character in the last match.
If the global flag (g) is not set, element 0 of the array contains the entire match, and elements 1 to n contain any submatches that occurred in the match.
This is equivalent to the exec method without setting the global flag. If the global flag is set, elements 0 to n contain all matches
example1 (the global flag is not set)
program code
var url=" http://msdn.microsoft.com:80/scripting/default.html ";
var reg=/(w+)://([^/:]+)(:d*)?([^#]*)/;
var myArray=url.match(reg);
RegExp.$1 //Return "http"
RegExp.$2 //Return "msdn.microsoft.com"
RegExp.$3 //Return ":80"
$egExp.$4 //Return "/scripting/default.html"
myArray //Return myArray[0]=" http://msdn.microsoft.com:80/scripting/default.html ",
myArray[1]="http",myArray[2]="msdn.microsoft.com",
myArray[3]=":80",myArray[4]="/scripting/default.html"
myArray.input //Return "http://msdn.microsoft.com:80/scripting/default.html"
myArray.index //Return 0
myArray.lastIndex //Return 51
example2 (global flag set)
program code
var url=" http://msdn.microsoft.com:80/scripting/default.html ";
var reg=/(w+)://([^/:]+)(:d*)?([^#]*)/g;
var myArray=url.match(reg);
RegExp.$1 //Return "http"
RegExp.$2 //Return "msdn.microsoft.com"
RegExp.$3 //Return ":80"
$egExp.$4 //Return "/scripting/default.html"
myArray //Return myArray=" http://msdn.microsoft.com:80/scripting/default.html "
myArray.input //Return "http://msdn.microsoft.com:80/scripting/default.html"
myArray.index //Return 0
myArray.lastIndex //Return 51
Note the difference after setting the global flag
If the global flag (g) is not set, element 0 of the array contains the entire match, and elements 1 to n contain any submatches that occurred in the match.
This is equivalent to the exec method without setting the global flag. If the global flag is set, elements 0 to n contain all matches
3. The exex method
runs a search on a string using a regular expression pattern and returns an array containing the results of the search.
program code
rgExp.exec(str)
If the exec method does not find a match, it returns null.
If it finds a match, the exec method returns an array and the properties of the global RegExp object are updated to reflect the match.
Element 0 of the array contains the complete match, while elements 1 to n contain any submatches that occur in the match.
This is equivalent to the match method without setting the global flag (g).
If the global flag is set for the regular expression, exec starts looking at the position indicated by the value of lastIndex.
If the global flag is not set, exec ignores the value of lastIndex and searches from the beginning of the string.
The array returned by the exec method has three attributes, namely input, index, and lastIndex.
The Input property contains the entire string being searched for.
The Index attribute contains the position of the matched substring in the entire searched string.
The LastIndex property contains the next position of the last character in the match.
example1 (no global flag set)
program code
var url=" http://msdn.microsoft.com:80/scripting/default.html ";
var reg=/(w+)://([^/:]+)(:d*)?([^#]*)/;
var myArray=reg.exec(url);
RegExp.$1 //Return "http"
RegExp.$2 //Return "msdn.microsoft.com"
RegExp.$3 //Return ":80"
$egExp.$4 //Return "/scripting/default.html"
myArray //Return myArray[0]=" http://msdn.microsoft.com:80/scripting/default.html ",
myArray[1]="http",myArray[2]="msdn.microsoft.com",
myArray[3]=":80",myArray[4]="/scripting/default.html"
myArray.input //Return "http://msdn.microsoft.com:80/scripting/default.html"
myArray.index //Return 0
myArray.lastIndex //Return 51
When the global flag (g) is not set, the match method is the same as the exec method
example2(set global flag)
program code
var url=" http://msdn.microsoft.com:80/scripting/default.html ";
var reg=/(w+)://([^/:]+)(:d*)?([^#]*)/;
var myArray=reg.exec(url);
RegExp.$1 //Return "http"
RegExp.$2 //Return "msdn.microsoft.com"
RegExp.$3 //Return ":80"
$egExp.$4 //Return "/scripting/default.html"
myArray //Return myArray[0]=" http://msdn.microsoft.com:80/scripting/default.html ",
myArray[1]="http",myArray[2]="msdn.microsoft.com",
myArray[3]=":80",myArray[4]="/scripting/default.html"
myArray.input //Return "http://msdn.microsoft.com:80/scripting/default.html"
myArray.index //Return 0
myArray.lastIndex //Return 51
4 The search method
returns the position of the first substring that matches the regular expression search content.
program code
stringOjb.search(rgExp)
The search method indicates whether a corresponding match exists.
If a match is found, the search method returns an integer value indicating the offset of the match from the beginning of the string.
If no match is found, -1 is returned.
example1
program code
var url=" http://msdn.microsoft.com:80/scripting/default.html ";
var reg=/(w+)://([^/:]+)(:d*)?([^#]*)/;
var flag=url.search(reg);
flag //return 0
RegExp.$1 //Return "http"
RegExp.$2 //Return "msdn.microsoft.com"
RegExp.$3 //Return ":80"
$egExp.$4 //Return "/scripting/default.html"
Neither the search nor test methods can update the global RegExp object, so RegExp.input, RegExp.index, and RegExp.lastIndex return undefined
5 The replace method
returns a copy of the string after text replacement based on the regular expression and
can update the global RegExp object.