Regular Expression parsing in JavaScript

Source: Internet
Author: User

Regular Expression parsing in JavaScript

The regular expression object contains a regular expression pattern ). It has the properties and methods used to match or replace a specific character (or character set combination) in a string (string) in the regular expression mode ). To add attributes to a single regular expression, you can use the regular expression constructor function ), the pre-configured regular expression has static properties (the predefined RegExp object has static properties that are set whenever any regular expression is used, I don't know if it is correct. I will list the original text. Please translate it by yourself ).

Create:
A text format or regular expression Constructor
Text Format:/pattern/flags
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
[Note] parameters in text format do not use quotation marks, but parameters in constructor must be enclosed in quotation marks. For example,/AB + c/I new RegExp ("AB + c", "I") implements the same function.
In the constructor, some special characters need to be converted (Add "\" before special characters "\"). For example, re = new RegExp ("\ w + ")

Special characters in Regular Expressions

Characters
\ As a conversion, that is, the characters after "\" do not follow the original meaning, such as/B/matching character "B ", when a backslash is added before B/\ B/, it is converted to 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,/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 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 in "$5.98, 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:

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, "<$1> ");
}

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 the Regular Expression of perl)

Attribute meaning of a regular expression object
$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

Regular Expression object Method
Meaning
Compile regular expression comparison
Exec search
Test
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.

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.