JavaScript: Application of regular expressions apply regular Expression object RegExp create regular expression
The RegExp object is used in JavaScript to express a regular expression. Before you use regular expressions, you first create a RegExp object :
var New REGEXP (Pattern,[flag]);
Description
Pattern: is a required option to represent a regular expression in string format.
Flag: As optional, is the matching option, the available values are as follows:
modifier |
Description |
I |
Performs a match that is not case sensitive. |
G |
Performs a global match (finds all matches rather than stops after the first match is found). |
M |
Performs multi-line matching. |
The user does not necessarily want to display the create RegExp, and can also implicitly create the RegExp object :
var oregp =/pattern/[flag]
Attention:
when you create an RegExp object using an explicit format, "\" in the regular expression is replaced with "\ \ " and the expression begins and ends without writing "/".
To determine if a string has a matching content
Description
The test () method is used to detect whether a string matches a pattern and returns true if the string contains matching text, otherwise false.
Instance:
Detects whether a user enters a mailbox that matches a pattern
<!DOCTYPE HTML><HTMLLang= "en"> <Head> <title></title> <MetaCharSet= "UTF-8"> <Metaname= "Viewport"content= "Width=device-width, initial-scale=1"> <Linkhref= "Css/style.css"rel= "stylesheet"> </Head> <Body> <form> <inputname= "Email"type= "text"ID= "Email"> <inputname= "Check"type= "button"value= "Detection"onclick= "Checkmail ()"> </form> <Script> functionCheckmail () {var Objreg = /\w+[@]{1}\w+[.] {1}\w+ / //mode varEmail=document.getElementById ("Email"). Value; if(objreg.test (email))//Use test for match detection, return TRUE or false {alert ("Email Compliance Specification") }Else{alert ("email does not conform to specifications") } } </Script> </Body></HTML>
Match detection on a string
Description
The exec () method, which takes one match detection of the specified string, gets the first match of the regular expression in the string and stores the result of the match and its sub-matches in the returned array.
Instance:
Detects the phone number and stores the result of the first match success and its sub-matching results in the returned array.
<Script> varObjreg=/1[35] (\d) (\d{8})/G; varostring="my phone 13111111111 it's phone 15222222222" var arr=objreg.exec (ostring); for(varI=0; I<Arr.length;i++) {document.write (arr[i]); } </Script>
Description
Here only the first match to, that is, 13111111111 This phone,15222222222 Although also meet, but this method only one match.
replaces the specified content in a string
Description
The replace () method can substitute the specified substring for content that matches the specified amount of regular expressions in the string.
Instance:
Turn all the numbers into X
<Script> varObjreg=/\d/G; varostring="my phone 13111111111 it's phone 15222222222" document.write (Ostring.replace (Objreg,'X')); </Script>
Match processing results
In JavaScript, when a regular expression is used for retrieval, the matching results are saved in the collection. This section explains the various processing in the collection, such as getting a matching position, getting a sub-match result, and getting a matching index.
get all matching information in a string
Description
The String.match () method returns an array that stores all the matching information in the string.
Instance:
Match all words with a second letter O
<Script> var objreg =/\wo (\w+)? / G; All words with a second letter of 0 varostring="Do you Lova MS?" vararr=Ostring.match (Objreg); if(arr!=NULL) { for(varI=0; I<Arr.length;i++) {document.write ("<li>"+arr[i])} } </Script>
gets the start address of the first match success
Description
The String.search () method is compared to the specified regular expression to get the position that matches the first occurrence of the content.
Instance:
Detects the first occurrence of a abcba similar form of string position
Code:
<Script> varObjreg=/(\d) (\d) \d\2\1/; varostring="11010111" varPOS=Ostring.search (Objreg); if(POS!=-1) { varcontent=objreg.exec (ostring); Alert ("Yes, we got the location."+POS+"content is"+content[0]); } </Script>
Effect:
Add:
Description
Back reference is used here.
- When a regular expression is grouped, each group is automatically assigned a group number, which can represent the group's expression.
- Grouping rules: Left to right, left parenthesis as flag, first group 1, second group 2, and so on.
- A reverse reference provides a convenient way to find repeating character groups. It can be thought of as a quick command to match the same string again.
The second way:
Regexp.index: Static property that returns the starting position of the first occurrence of a string
JavaScript: Learning Notes (3)--application of regular expressions