JavaScript Regular Expressions use detailed parameters

Source: Internet
Author: User

Regular Expression constructor: new RegExp ("pattern" [, "flags"]);
Parameter description:
Pattern -- a regular expression text
Flags -- if it exists, it will be the following values:
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 "\" are not interpreted as original meaning, such as/B/matching character "B ", when a backslice rod is added in front of B/\ B /,

Match the boundary of a word.
-Or-
Restores the function characters of a regular expression. For example, if "*" matches the previous metacharacters 0 or multiple times,/a */matches a, aa, aaa, after "\" is added,/\*/

Only "a *" will be matched *".
^ 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 first metacharacters 0 or multiple times./ba */match B, ba, baa, baaa
+ Match the first metacharacters once or multiple times./ba */match the first metacharacters with ba, baa, and 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 matches x or y
{N} exact match n times
{N,} match more than n times
{N, m} matches n-m times
[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 control character, // \ cM/matches Ctrl-M
\ D matches one word character,/\ d/=/[0-9]/
\ D matches a non-word 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 to/[^ \ 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 [a-zA-Z0-9]
\ W matches a character that cannot comprise a word, such as [\ W] matching $ in "$5.98", equal to [^ 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 ){
Var myReg =/<(\ w +)> /;
Return strInput. replace (myReg, "<$1> ");
}
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;
}


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"

Related Article

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.