[Reprint] JavaScript-Regular Expression

Source: Internet
Author: User
Javascript - Regular Expression
The regular expression object contains a regular expression pattern ). It has the properties and methods that use the regular expression mode to match or replace a string with a specific character (or character set combination ).

Regular Expression constructor: New Regexp ("pattern" [, "Flags"]);
Parameter description:
Pattern -- A regular expression text
Flags -- If yes, the values are as follows:
G: Global match
I: case insensitive
GI: combination of the above

In the constructor, some special characters need to be converted (Add "\" before special characters). Special characters in Regular Expressions:
Characters
\ Conversion, that is, the characters after "\" do not follow the original meaning, as shown in / B / Match the character "B". When a backslice is added before B / \ B / ,

Match the boundary of a word.
- Or -
Returns the function characters of a regular expression, such as * "Match the first metacharacter zero or multiple times, / A */ Match A, AA, AAA, and add, / A \ */

Only * ".
^ Matches the beginning of an input or line, /^ A / Matches "an A" instead of "An a".
$ Matches the end of an input or a row, / A $ / Matches "an A" instead of "An a".
* Match the previous metacharacters 0 or multiple times, / Ba */ Match B, Ba, Baa, baaa
+ Match the previous metacharacters once or multiple times, / Ba */ Match Ba, Baa, baaa
? Match the first metacharacters 0 or 1 time, / Ba */ Match B, Ba
(X) Match x save X in name $ 1 $9 variable
X | Y matches X or Y
{N} Exact match n times
{N ,} Match more than N times
{N, m} Match n - MB
[Xyz] character set (character set) that matches any one of the set's characters (or metacharacters)
[ ^ XYZ] does not match any character in this set
[\ B] matches a return character.
\ B matches the boundary of a word
\ B matches the non-boundary of a word
\ CX here, X is a controller, / \ Cm / Match CTRL - M
\ D matches one word character, / \ D /   =   / [ 0 - 9 ] /
\ D matches a non-wordcount character, / \ D /   =   / [ ^ 0 - 9 ] /
\ N matches a linefeed.
\ R matches a carriage return.
\ S matches a blank character, including \ n, \ r, \ f, \ t, \ v, etc.
\ S matches a non-blank character, equal / [ ^ \ N \ f \ r \ t \ v] /
\ T matches a tab
\ V matches a straight-heavy Tab
\ W matches a character that can comprise a word (alphanumeric, this is my free translation, including numbers), including underscores, such as [\ W] matching "$ 5.98 "

5 In, equal to [ - Za - Z0 - 9 ]
\ W matches a character that cannot constitute a word, for example, [\ W] matches "$ 5.98 $ In ", equal [ ^ A - Za - Z0 - 9 ].


After talking about this, let's look at some examples of the practical application of Regular Expressions:
HTML code shielding
Function Mask_htmlcode (strinput) {
VaRMyreg= /<(\ W+)> )〉/;
ReturnStrinput. Replace (myreg, "<$1> ");
}
E - Mail address verification:
Function Test_email (stremail) {
VaR Myreg =   /^ [_ - Z0 - 9 ] + @ ([_ - Z0 - 9 ] + \.) + [ - Z0 - 9 ] {2,3} $ / ;
If (Myreg. Test (stremail )) Return   True ;
Return   False ;
}


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. The value of other attributes is

Different conditions are assigned with different values. 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 meaning
$ 1 $ 9 If it exists, it is a matched substring.
$ _ See Input
$ * See multiline
$ & See lastmatch
$ + See lastparen
$ 'See leftcontext
$ ''See rightcontext
Constructor creates a special function prototype of an object.
Whether global matches the entire string (bool type)
Whether to ignore case sensitivity when matching ignorecase (bool type)
Input matched string
Lastindex the last matched Index
The substring enclosed in the last square brackets of lastparen.
The last time leftcontext matches a substring with the left
Whether multiline matches multiple rows (bool type)
Prototype allows attributes to be attached to an object.
Rightcontext: The last matched substring with the right
Source regular expression mode
Lastindex the last matched Index

Method of the regular expression object:
Meaning
Compile regular expression comparison
Exec search
Test
Tosource returns the definition of a specific object (literal

The 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"

 

 

Onkeyup = " This. value = This. value. Replace (/[^ 0-9 \ +]/g ,'') " Onafterpaste = " This. value = This. value. Replace (/[^ 0-9 \ +]/g ,'') "

Function Checkfloat () {
VaR Result = True ;
Alert (event. keycode );
Result = (Event. keycode =   9   | Event. keycode =   45   |
Event. keycode =   46   | Event. keycode =   107   |
Event. keycode =   37   |
Event. keycode =   39   | (Event. keycode =   187   && Event. shiftkey =   True ) |
(Event. ctrlkey && (Vent. keycode =   67   | Vent. keycode =   86 )) |
Event. keycode =   8   |
Event. keycode =   110   |
Event. keycode =   190   |
(Event. keycode > = 96   && Event. keycode <= 105 ) |
(Event. keycode > = 48   && Event. keycode <= 57 ));
Event. returnvalue = Result;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.