Just as a string literal is defined as a character enclosed in quotation marks, the regular expression literal is also defined as a character that is contained between a pair of slashes (/). Therefore, JavaScript may contain the following code:
The code is as follows |
Copy Code |
var pattern =/s$/; |
This line of code creates a new RegExp object and assigns it to the variable parttern. This particular RegExp object matches all strings that end with the letter "s". You can also define an equivalent regular expression by using regexp (), as follows:
The code is as follows |
Copy Code |
var pattern = new RegExp ("s$"); |
Tips and comments
Important: REGEXP objects that do not have a flag G and do not represent a global schema cannot use the Lastindex property.
Tip: If you start retrieving another new string after successfully matching a string, you need to manually set this property to 0
Example
The following example illustrates the use of the test method:
The code is as follows |
Copy Code |
<script language=jscript> function Testdemo () { try{ var re= "ABC" var s=/a/ This sets the regular expression Alert (S.test (RE))//If included then return True }catch (e) {alert ("Err")} } Testdemo () </script> |
Cases
The code is as follows |
Copy Code |
<script type= ' Text/javascript ' > var retest =/^aid= (. *)/ig; var adata = [' aid=^$ ', ' aid=^$ ', ' aid=^$ ', ' aid=^$ ']; For (Var i=0, l=adata.length i<l; i++) { Retest.lastindex = 0; Alert (Retest.test (Adata[i])); } </script> |
In this way, the result is right ~, of course
In addition, Moxie students said the method is actually more effective, since you do not need g, then why set g?
The code is as follows |
Copy Code |
<script type= ' Text/javascript ' > var retest =/^aid= (. *)/I; var adata = [' aid=^$ ', ' aid=^$ ', ' aid=^$ ', ' aid=^$ ']; For (Var i=0, l=adata.length i<l; i++) { Alert (Retest.test (Adata[i])); } </script>< li> |
Instance
The code is as follows |
Copy Code |
The test method, which tests the string, returns True when it conforms to the pattern, or returns false var re =/he/;//simplest regular expression that matches the word he var str = "he"; Alert (Re.test (str));//true str = "we"; Alert (Re.test (str));//false str = "he"; Alert (Re.test (str));//false, uppercase, if you want to match case, you can specify I flag (i is ignorecase or case-insensitive representation) re =/he/i; Alert (Re.test (str));//true str = "certainly! He loves her! "; Alert (Re.test (str));//true, as long as the inclusion of he is in accordance with, if you want to just he or he, cannot have other characters, you can use ^ and $ Re =/^he/i;//character (^) represents the start position of a character Alert (Re.test (str));//false, because he was not at the beginning of str str = "He is a good boy!"; Alert (Re.test (str));//true,he is the character start position, and you need to use the $ Re =/^he$/i;//$ indicates the end position of the character Alert (Re.test (str));//false str = "he"; Alert (Re.test (str));//true Of course, this does not reveal how powerful the regular expression is, because we can use = = or indexof in the example above Re =/s/;//s matches any white space character, including spaces, tabs, page breaks, and so on str= "user name";//username contains spaces Alert (Re.test (str));//true str = "user name";//user name contains tab Alert (Re.test (str));//true Re=/^[a-z]/i;//[] matches any character within a specified range, where the English alphabet is matched, case-insensitive Str= "VariableName";//variable name must begin with a letter Alert (Re.test (str));//true Str= "123ABC"; Alert (Re.test (str));//false |
The direct measure character of a regular expression
Character matching
________________________________
Alpha-numeric characters themselves
F Change page character
N line Feed
R carriage
T tab
V Vertical Tabs
/One/Direct quantity
A direct amount
. One. Direct quantity
* A * Direct quantity
+ One + Direct quantity
? One? Direct quantity
| One | Direct quantity
(A direct amount
) A direct amount
[One [Direct quantity
] A direct quantity
{One {direct quantity
} A direct amount
XXX ASCII code characters specified by decimal number xxx
XNN ASCII code characters specified by the hexadecimal number nn
CX control character ^x. For example, CI is equivalent to T, CJ is equivalent to n
Character matching
____________________________________________________
[...] Any character that is within the parentheses
[^...] Any character not in parentheses
. Any character other than a line break, equivalent to [^n]
W any single word character, equivalent to [a-za-z0-9]
W any non-word character, equivalent to [^a-za-z0-9]
s any blank character, equivalent to [t n R F v]
S any non-white-space character, equivalent to [^ t n r F v]
d any number, equivalent to [0-9]
D any character other than a number, equivalent to [^0-9]
[b] A backspace direct quantity (special case)
Character meaning
__________________________________________________________________
{n, m} matches the previous item at least n times, but cannot exceed m times
{n ,} matches n times before, or multiple times
{n} matches exactly n times before
? Matches the previous item 0 or 1 times, which means the previous item is optional. The equivalent of {0, 1}
+ matches the previous 1 or more times, equivalent to {1,}
* matches the previous 0 or more times. Equivalent to { 0,}