Regular expression parsing _ regular expressions in JavaScript

Source: Internet
Author: User
Tags character set function prototype
Regular expression parsing in JavaScript

The regular expression (regular expression) object contains a regular expression pattern. It has attributes (properties) and methods (methods) that match or replace a particular character (or character set) in a string in a regular expression pattern. To add a property to a single regular expression, you can use the regular expression constructor (constructor function), regardless of when a preset regular expression that is invoked has a static property (the predefined RegExp object has Static properties that are set whenever any regular expression is used, I do not know if I turned the right, the original list, please self-translation.

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 literal
Flags--if present, will be the following values:
G: Global Match
I: Ignore case
GI: Above combination
[note] The arguments in the text format do not use quotation marks, but the arguments that are used when using the constructor require quotes. such as:/ab+c/i new RegExp ("Ab+c", "I") is the implementation of the same function.
In constructors, some special characters need to be transferred (plus "\" before a special character). such as: Re = new RegExp ("\\w+")

Special characters in regular expressions

Character implication
* As a turn, that is, usually after the "\" character does not interpret the original meaning, such as/b/match the character "B", when the B is preceded by a backslash/\b/, turn to match the boundary of a word.
Or
A restore of a regular expression feature character, such as "*" matches its preceding metacharacters 0 or more times,/a*/will match a,aa,aaa, and after "\",/a\*/will only match "a *".

^ matches the beginning of an input or line,/^a/matches "an A" and does not match "an A"
$ matches the end of an input or line,/a$/matches "an A" and does not match "an A"
* Match the preceding metacharacters 0 or more times,/ba*/will match b,ba,baa,baaa
+ Match the preceding metacharacters 1 or more times,/ba*/will match ba,baa,baaa
? Match the preceding metacharacters 0 or 1 times,/ba*/will match B,ba
(x) match x Save x in a variable named $1...$9
X|y match x or Y
{n} exact match n times
{n,} matches n times above
{n,m} matching n-m times
[XYZ] Character set (character set) that matches any one by one characters (or metacharacters) in this collection
[^XYZ] does not match any one of the characters in this collection
[\b] matches a backspace
\b Match the bounds of a word
\b Match the non-boundary of a word
\CX here, X is a control character,/\cm/match ctrl-m
\d matches a character number character,/\d/=/[0-9]/
\d matches a non-word number character,/\d/=/[^0-9]/
\ n matches a newline character.
\ r matches a return character
\s matches a blank character, including \n,\r,\f,\t,\v, etc.
\s matches a non-white-space character equal to/[^\n\f\r\t\v]/
\ t matches a tab
\v matches a heavy-straight tab
\w matches a character that can make up a word (alphanumeric, which is my transliteration, with numbers), including underscores, like [\w] matches 5 in "$5.98", equal to [a-za-z0-9]
\w matches a character that cannot be composed of words, such as [\w] matches $ in "$5.98", equal to [^a-za-z0-9].



Having said so much, let's look at some examples of the actual application of regular expressions:

e-mail 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;
}
Masking of HTML code
function Mask_htmlcode (strinput) {
var Myreg =/< (\w+) >/;
Return Strinput.replace (Myreg, "<$1>");
}

Properties and methods of regular expression objects
Predefined regular expressions have the following static properties: Input, Multiline, Lastmatch, Lastparen, Leftcontext, Rightcontext, and $ $.
Where input and multiline can be preset. The values of other properties are assigned different values according to different conditions after the exec or test method has been executed.
Many attributes have both long and short (Perl-style) two names, and the two names point to the same value. (JavaScript simulates Perl's regular expression)

The meaning of the property property of the regular Expression object
$1...$9 if it exists, it is the substring of the match.
$_ See input
$* See Multiline
$& See Lastmatch
$+ See Lastparen
$ ' See Leftcontext
$ ' See Rightcontext
constructor creates a special function prototype for an object
Whether global matches in the entire string (bool type)
Whether to ignore case (bool type) when IgnoreCase matches
Input is matched string
Lastindex the last matching index
Lastparen a substring enclosed in the last bracket
Leftcontext last match with left substring
Multiline whether multiple rows are matched (bool type)
Prototype allows attributes to be attached to objects
Rightcontext last match with the right substring
SOURCE Regular expression pattern
Lastindex the last matching index


Methods of regular Expression objects
Method meaning
Comparison of compile regular expressions
EXEC Perform Lookup
Test to match
Tosource returns the definition of a particular object (literal representing), whose value can be used to create a new object. Overloaded Object.tosource method is obtained.
ToString returns a string for a particular object. Overloaded Object.ToString method is obtained.
valueof returns the original value of a particular object. The method of overloading object.valueof
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.