Regular expression syntax
Escape Character |
Description |
General characters |
Except. $ ^ {[(|) * +? /, And other characters match themselves. |
Character |
Meaning |
/ |
In turn, that is, the characters after "/" are not interpreted as original meaning, such as/B/matching character "B ", when B is added to the front of the backslice bar // B/, it is converted to match the boundary of a word. -Or-restore the Regular Expression Feature characters. For example, if "*" matches the previous metacharacters 0 or multiple times,/a */matches a, AA, AAA, after "/" is added,/a/*/will only match "*". |
^ |
Matches the beginning of an input or a line,/^ A/matches "an A", but does not match "an" |
$ |
Matches the end of an input or line,/a $/matches "an A", but does not match "an" |
* |
Match the previous metacharacters 0 or multiple times (any number of times),/a */will match zero or multiple A,/Ba */will match B, Ba, Baa, baaa |
+ |
Match the previous metacharacters once or multiple times./A +/will match one or more A,/Ba */will match Ba, Baa, baaa |
? |
Match the first metacharacters 0 or 1 times,/Ba */will match B, Ba |
(X) |
Match X and save X in the variable $1... $9. |
X | y |
Match X or Y |
{N} |
Exact match n times |
{N ,} |
Match more than N times |
{N, m} |
Match N-m times |
[Xyz] |
Character set (Character Set), which matches any one of the characters (or metacharacters) in the set) |
[^ XYZ] |
Does not match any character in this set |
[/B] |
Match a return character |
/B |
Match the boundary of a word |
/B |
Match non-boundary of a word |
/CX |
Here, X is a controller, // cm/matches Ctrl-m |
/D |
Matches a number, which is equivalent to [0-9]. |
/D |
Match a non-numeric character, equivalent to [^ 0-9] |
/N |
Match A linefeed |
/R |
Match a carriage return. |
/S |
Matches a blank character, including/N,/R,/F,/t,/V, etc. |
/S |
Match a non-blank character, equivalent to [^/n/F/R/T/V] |
/T |
Match a tab |
/V |
Match a Duplicate Tab |
/W |
Number, letter, underline, equivalent to [a-zA-Z0-9 _] |
/W |
Non/W, equivalent to [^ a-zA-Z0-9 _] |
Example:
Email address verification:
Function test_email (stremail)
{
VaR myreg =/^ [_ a-z0-9] + @ ([_ a-z0-9] +/.) + [a-z0-9] {2, 3} $ /;
If (myreg. Test (stremail) return true;
Return false;
}
HTML code shielding
Function mask_htmlcode (strinput)
{
VaR myreg =/<(/W +)> /;
Return strinput. Replace (myreg, "& lt; $1 & gt ;");
}
C #
bool IsValidEmail(string strIn){ // Return true if strIn is in valid e-mail format. return Regex.IsMatch(strIn, @"^([/w-/.]+)@((/[[0-9]{1,3}/.[0-9]{1,3}/.[0-9]{1,3}/.)|
(([/w-]+/.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(/]?)$ " ) ;
}
Attributes and methods of a regular expression object
Predefined regular expressions have the following static attributes: input, multiline, lastmatch, lastparen, leftcontext, rightcontext, and $1 to $9. Input and Multiline can be pre-set. Values of other attributes are assigned different values based on different conditions after the exec or test method is executed. Many attributes have both long and short (Perl style) names, and these two names point to the same value. (JavaScript simulates Regular Expressions in Perl) attributes of a regular expression object
Attribute |
Description |
$1... $9 |
If it exists, it is a matched substring. |
$ _ |
See Input |
$ * |
See multiline |
$ & |
See lastmatch |
$ + |
See lastparen |
$' |
See leftcontext |
$' |
See rightcontext |
Constructor |
Create a special function prototype for an object |
Global |
Match in the entire string (bool type) |
Ignorecase |
Whether to ignore the case sensitivity when matching (bool type) |
Input |
Matched string |
Lastindex |
Last matched Index |
Lastparen |
Substring enclosed in parentheses |
Leftcontext |
The last match takes the left substring |
Multiline |
Whether multi-row matching is performed (bool type) |
Prototype |
Allow attributes to be attached to objects |
Rightcontext |
The last matched substring to the right |
Source |
Regular Expression Mode |
Lastindex |
Last matched Index |
Regular Expression object Method
Method |
Description |
Compile |
Regular Expression comparison |
Exec |
Execute search |
Test |
Matching |
Tosource |
Returns the definition of a specific object (literal representing). Its value can be used to create a new object. This is obtained by reloading the object. tosource method. |
Tostring |
Returns the string of a specific object. The result is obtained by reloading the object. tostring method. |
Valueof |
Returns the original value of a specific object. Obtain the value of the object. valueof method. |
Example
<Script language = "JavaScript">
VaR myreg =/(/W +)/S (/W +)/; var STR = "John Smith ";
VaR newstr = Str. Replace (myreg, "$2, $1 ");
Document. Write (newstr );
</SCRIPT>
Output "Smith, John"