Regular Expression (Basic + instance)

Source: Internet
Author: User

The regular expression is a regular expression. It seems that English is better understood than Chinese, that is, the regular expression is checked for non-conforming characters !! Regular expressions have a powerful and complex object RegExp, which is provided in Javascript1.2 or later.

Let's take a look at the introduction of regular expressions:

A regular expression object is used to regulate a standard expression (that is, the expression operator does not meet specific requirements, such as whether it is an Email address format or not ), it has the attributes and methods used to check whether the given string meets the rules. In addition, the attributes of individual regular expression objects created using the RegExp constructor have pre-defined static attributes of Regular Expression objects, and you can use them at any time.

Core objects:
Available in Javascript 1.2 and NES 3.0 and later versions.
The toSource method is added in Versions later than Javascript 1.3.

Creation method:

Text format or RegExp constructor function.
The text format is as follows:
/Pattern/flags:/mode/flag

The constructor function method is as follows:
New RegExp ("pattern" [, "flags"]) is new RegExp ("Mode" [, "flag"])
Parameters:
Pattern)
Indicates the text of a regular expression.

Flags)
If this option is specified, flags can be one of the following nominal values:
G: global match (exact match)
I: ignore case (case insensitive)
Gi: both global match and ignore case (matching all possible values, case Insensitive)

Note: Do not Mark parameters in text format with quotation marks, while parameters in constructors must be marked with quotation marks. So the following
The expression creates the same regular expression:
/AB + c/I
New RegExp ("AB + c", "I ")

Description:
When using constructor, you must use a normal string to avoid rules (adding the leading character \ To the string.
For example, the following two statements are equivalent:
Re = new RegExp ("\ w + ")
Re =/\ w +/

The following provides a complete list and description of special characters that can be used in regular expressions.

Table 1.3: special characters in Regular Expressions:

Character \
Meaning: For characters, it usually indicates the literal meaning, indicating that the next character is a special character, \ is not interpreted.
For example:/B/matches the character 'B'. By adding a backslash \, that is,/\ B/, before B, the character becomes a special character, indicating
Match the dividing line of a word.
Or:
For a few characters, it is generally described as special. It is pointed out that the subsequent characters are not special, but should be interpreted literally.
For example, * is a special character that matches any character (including 0 characters). For example,/a */indicates that it matches 0 or multiple a characters.
To match the literal *, add a backslash before a. For example,/a \ */matches 'A *'.

Character ^
Meaning: The matched characters must be at the frontend.
For example,/^ A/does not match 'A' in "an A,", but matches 'A' in the top of "An '.

Character $
Meaning: similar to ^, it matches the last character.
For example,/t $/does not match 'T' in "eater", but matches 'T' in "eat '.

Character *
Meaning: match the first character of * 0 or n times.
For example,/bo */matches 'boooo' in "A ghost booooed" or 'B' in "A bird warbled", but does not match "A goat g
Any character in runted.

Character +
Meaning: match the character before the plus sign once or n times. It is equivalent to {1 ,}.
For example,/a +/matches all 'A' in "candy" and "caaaaaaandy '.

Character?
Meaning: match? The first character is 0 or 1 time.
Example:/e? Le? /Match 'El' in "angel" and 'le' in "angle '.

Character.
Meaning: (decimal point) match all single characters except line breaks.
For example,/. n/matches 'any' and 'on' in "nay, an apple is on the tree", but does not match 'nay '.

Character (x)
Meaning: Match 'X' and record the matched value.
For example,/(foo)/matches and records 'foo' in "foo bar '. Matching substrings can be returned by the element [1],..., [n] In the result array.
Returned by RegExp object attributes $1,..., $9.

Character x | y
Meaning: Match 'X' or 'y '.
For example,/green | red/matches 'green' in "green apple" and 'red' in "red apple '.

Character {n}
Meaning: Here n is a positive integer. Match the previous n characters.
For example:/a {2}/does not match 'A' in "candy,", but matches all 'A' in "caandy," and the first two in "caaandy ."
'A '.

Character {n ,}
Meaning: Here n is a positive integer. Match at least n FIRST characters.
For example,/a {2,} does not match 'A' in "candy", but matches all 'A' in "caandy" and "caaaaaaandy'

Character {n, m}
Meaning: both n and m are positive integers. Match at least n characters at most before m.
For example,/a {}/does not match any character in "cndy", but matches the first two characters in "candy," 'A', "caandy ,"
'A' and "caaaaaaandy" are the first three 'A'. Note: Even if "caaaaaaandy" contains many 'A', it only matches the first three.
(Aaa ".

Character [xyz]
Meaning: A one-character list that matches any character in the list. You can use a hyphen to indicate a character range.
For example, [abcd] is the same as [a-c. They match 'B' in "brisket" and 'C' in "ache '.

Character [^ xyz]
Meaning: A character complement, that is, it matches everything except the listed characters. You can use a hyphen to indicate
Character range.
For example, [^ abc] is equivalent to [^ a-c]. They first match 'R' in "brisket" and 'H' in "chop '.

Character [\ B]
Meaning: match a space (do not confuse with \ B)

Character \ B
Meaning: match the boundary of a word, such as a space (do not confuse it with [\ B)
For example,/\ bn \ w/matches 'no' in "noonday",/\ wy \ B/matches 'ly 'in "possibly yesterday '.

Character \ B
Meaning: match the non-dividing line of a word
For example,/\ w \ Bn/matches 'on' in "noonday",/y \ B \ w/matches 'Ye 'in "possibly yesterday '.

Character \ cX
Meaning: X is a control character. Matches the control character of a string.
For example,/\ cM/matches the control-M in a string.

Character \ d
Meaning: matching a number is equivalent to [0-9].
For example,/\ d/or/[0-9]/matches '2' in "B2 is the suite number '.

Character \ D
Meaning: match any non-number, which is equivalent to [^ 0-9].
For example,/\ D/or/[^ 0-9]/matches 'B' in "B2 is the suite number '.

Character \ f
Meaning: match a form character

Character \ n
Meaning: match a linefeed.

Character \ r
Meaning: match a carriage return.

Character \ s
Meaning: match a single white space character, including space, tab, form feed, line feed, equivalent to [\ f \ n \ r \ t \ v].
For example,/\ s \ w */matches 'bar' in "foo bar '.

Character \ S
Meaning: match a single character except the white space character, which is equivalent to [^ \ f \ n \ r \ t \ v].
For example,/\ S/\ w * matches 'foo' in "foo bar '.

Character \ t
Meaning: match a tab

Character \ v
Meaning: match a top Tab

Character \ w
Meaning: match all numbers, letters, and underscores, equivalent to [A-Za-z0-9 _].
For example,/\ w/matches 'A' in "apple,", "$5.28,", "5", and "3D '.

Character \ W
Meaning: match other characters except numbers, letters, and underscores, equivalent to [^ A-Za-z0-9 _].
For example:/\ W/or/[^ $ A-Za-z0-9 _]/matches '%' in "50% '.

Character \ n
Meaning: Here n is a positive integer. Match the n value of the last substring of a regular expression (left parentheses ).

For example:/apple (,) \ sorange \ 1/match 'apple, orange, cherry, peach. ".
There is a more complete example.
Note: If the number in the left parentheses is smaller than the number specified by \ n, \ n removes the octal escape of a row as the description.

Characters \ ooctal and \ xhex
Meaning: here \ ooctal is an escape value of octal, and \ xhex is a hexadecimal escape value, which can be
An ASCII code is embedded in a regular expression.

When the expression is checked, the text symbol provides the method to edit the regular expression. Use text symbols to make regular expressions
The formula is constant. For example, if you use text symbols in a loop to construct a regular expression
Compile multiple times.
Regular Expression object constructor, for example, new RegExp ("AB + c"), provides runtime compilation for regular expressions. When you know
When the expression pattern changes, you should use the constructor, or you do not know the pattern of the regular expression.
When the source is obtained, such as when the user inputs. Once you have defined a regular expression, it can be used anywhere,
It can also be changed. You can use the compilation method to compile a new regular expression for reuse.
A separated pre-defined RegExp object can be used in each window; that is, each separated Javascript thread runs
To obtain the RegExp object. Because each script cannot be interrupted in a thread, this ensures that different scripts will not be overwritten.
The value of the RegExp object.
Predefined static attributes of a RegExp object: input, multiline, lastMatch, lastParen, leftContext,
RightContext, and from $1 to $9. The input and multiline attributes can be preset. The values of other static attributes are executing individual regular expressions.
After the exec and test methods of the expression object, and set after the string match and replace methods are executed.

Attribute
Note that several attributes of the RegExp object have both long names and short names (such as Perl ). These names all point to the same value. Perl is
A programming language, while Javascript imitates its regular expression.

Attribute $1,..., $9
Obtain matched substrings, if any.

Attribute $ _
Reference input

Attribute $ *
See multiline

Attribute $ &
Refer to lastMatch

Attribute $ +
Refer to lastParen

Attribute $'
Refer to leftContext

Attribute $'
Refer to rightContext

Attribute constructor
Specify the object prototype Function

Attribute global
Determines whether to test whether the regular expression cannot match all strings or only conflicts with the first one.

Attribute ignoreCase
Determines whether to ignore the case sensitivity when trying to match a string

Attribute input
When the regular expression is matched, it is the opposite string.

Property lastIndex
Determine where the next match starts

Attribute lastMatch
Last matched character

Property lastParen
The last parenthesized when the substring is matched, if any.

Attribute leftContext
The substring before the last match.

Attribute multiline
Whether to search for strings in multiple rows.

Attribute prototype
Allow attributes to be appended to all objects

Attribute rightContext
The substring after the last match.

Attribute source
Mode text

 

 

Method
Compile Method
Compile a regular expression object

Exec Method
Run regular expression matching

Test Method
Test Regular Expression matching

ToSource Method
Returns the text description of an object. You can use this value to create a new object. Ignore Object. toS
Ource method.

ToString Method
Returns a string that describes the specified Object, regardless of the Object. toString Object.

ValueOf Method
Returns the original value of the specified diagonal. The Object. valueOf method is not considered.

In addition, this object inherits the watch and unwatch methods of the object.

 

Example:
Example 1: The following sample script uses the replace method to convert words in a string. In the replaced text, the script uses the Global RegExp
The value of the $1 and $2 attributes of the object. Note: When passed as the second parameter to the replace method, the $ attribute name of the RegExp object
Name.
<Script LANGUAGE = "Javascript1.2">
Re =/(\ w +) \ s (\ w + )/;
Str = "John Smith ";
Newstr = str. replace (re, "$2, $1 ");
Document. write (newstr)
</Script>
Result: "Smith, John ".

Example 2: In the following sample script, RegExp. input is set by the Change event processing handle. In the getInfo function, the exec Method
Use the value of RegExp. input as its parameter. Note that RegExp has a $ attribute preset.

<Script LANGUAGE = "Javascript1.2">
Function getInfo (abc)
{
Re =/(\ w +) \ s (\ d + )/;
Re.exe c (abc. value );
Window. alert (RegExp. $1 + ", your age is" + RegExp. $2 );
}
</Script>

Enter your surname and age, and press Enter.
<FORM> <input type = "TEXT" NAME = "NameAge" onChange = "getInfo (this);"> </FORM>
</HTML>

$1,..., $9 attributes
Match a substring enclosed in parentheses, if any.
Is the RegExp attribute
Static, read-only

Available in Javascript 1.2, NES 3.0 and later versions
Description: Because input is a static attribute, it is not an attribute of an individual regular expression object. You can use RegExp. input to access this
Attribute.

The number of substrings that can be added with parentheses is unrestricted, but the regular expression object can only keep the last nine strings. If you want to access all
The matching string in parentheses. You can use the returned array.

These attributes can be used to replace the strings (output results) with the RegExp. replace method ). When this method is used
First, consider the RegExp object. The following is an example. When the regular expression does not contain parentheses, the script is interpreted as $ n literal.
. (Here n is a positive integer ).

For example:
The following script uses the replace method to change the position of words in a string. In the replaced text string, the script uses a regular expression.
The value of the $1 and $2 attributes of the RegExp object. Note: When they pass parameters to the replace method, $ attribute's
The name of the RegExp object.
<Script LANGUAGE = "Javascript1.2">
Re =/(\ w +) \ s (\ w + )/;
Str = "John Smith ";
Newstr = str. replace (re, "$2, $1 ");
Document. write (newstr)
</Script>
The output result is Smith and John.

<Regular Expression details>

For the following newly added objects that are not regular expressions, see the corresponding Javascript Object Attributes $ _ attribute reference input $ * attribute
For details about multiline $ & attributes, refer to lastMatch $ + attributes refer to lastParen $ 'attributes
Refer to the leftContext $ 'attribute. Refer to the rightContext compile method to compile a regular expression object during script running.
The RegExp method provides the syntax in Javascript 1.2 and NES 3.0 and later versions:
Regexp. compile (pattern [, flags]) is the name of a number: regexp regular expression. It can be a variable name or a text string.
The definition Text of the regular expression pattern. If flags is specified, it can be one of the following: "g": matches all possible strings.
"I": case-insensitive "gi": matches all possible strings and case-insensitive descriptions:
Use the compile method to compile a regular expression created with the RegExp constructor function. This way
It forces the regular expression to be compiled only once instead of once every time a regular expression is encountered. When you confirm that the regular expression can
When it remains unchanged, you can use the compile method to compile it (after obtaining its matching mode), so that it can be used multiple times in the script.
You can also use the compile method to change the regular expression during running. For example, if the regular expression changes,
You can use the compile method to recompile the object to improve efficiency.
This method changes the values of the source, global, and ignoreCasesource attributes of the regular expression. Constructor
Specifies the function used to create an object prototype. Note that the value of this attribute is provided by the function itself, rather than the name. Property of a string containing RegExp.
In Javascript 1.1, NES 2.0 and later provide the ECMA version ECMA-262 Description: Refer to Object. constructor.
The exec method runs the matching search on the specified string. Returns an array of results. Is the RegExp Method
In Javascript 1.2, NES 3.0 and later versions provide the Syntax: regexp.exe c ([str]) regexp ([str]).
Parameter: regexp, the name of the regular expression. It can be a variable name or a text definition string.
Str, which matches the string of the regular expression. If omitted, the value of RegExp. input is used.
Description: As in the syntax, execute of the expression can be directly called (using regexp.exe c (str) or indirectly called (using regexp (str )).
If you just run it to find out whether it matches, you can use the String SEARCH method.
If the matching is successful, the exec method returns an array and updates the value of the regular expression object attribute with the pre-defined Regular Expression object and RegExp. If the matching fails, the exec method returns null.
See the following example: <script LANGUAGE = "Javascript1.2"> file: // matches one B, one or more d, and another B.
File: // case-insensitive myRe =/d (B +) (d)/ig; myArray = myRe.exe c ("cdbBdbsbz ");
</Script> The following is the returned value of the script: Object attribute/Index Description Example.
MyArray

MyArray content ["dbBd", "bB", "d"]
Index
Match index 1 based on 0
Input
Original string cdbBdbsbz
[0]
Last matched character dbBd
[1],... [n]
A matching string enclosed in parentheses, if any. Do not limit the number of parentheses. [1] = bB
[2] = d
MyRe
LastIndex
Index value 5 of the next matching operation
IgnoreCase
Specifies whether "I" is used to ignore case sensitivity. true
Global
Indicates whether to use the "g" flag to match all possible strings. true
Source
Define the pattern text string d (B +) (d)
RegExp
LastMatch $ &
Last matched character dbBd
LeftContext $ \ Q
The latest matching substring c
RightContext $'
Bsbz
$1,... $9
Matching substring in parentheses, if any. The number of parentheses is not limited, but RegExp can only retain the last 9 $1 = bB
$2 = d
LastParen $ +
The last matching substring with parentheses, if any, d

If your regular expression uses the "g" mark, you can use the exec method multiple times to continuously match the same string. When you do this
The New Match starts from the substring determined by the lastIndex attribute value of the regular expression. For example, assume that you use the following script:
<Script LANGUAGE = "Javascript1.2"> myRe =/AB */g; str = "abbcdefabh"
MyArray = myRe.exe c (str );
Document. writeln ("Found" + myArray [0] + ". Next match starts at" + myRe. lastIndex)
MySecondArray = myRe.exe c (str );
Document. writeln ("Found" + mySecondArray [0] + ". Next match starts at" + myRe. lastIndex)
</Script> the script displays the following results: Found abb. Next match starts at 3
Found AB. Next match starts at 9 example:
In the following example, the user enters a name and the script performs the matching operation based on the input. Then, check the array to see if it matches the names of other users.
This script assumes that the name of A registered user has been saved in array A and may be obtained from A database. <HTML>
<Script LANGUAGE = "Javascript1.2"> A = ["zhao", "qian", "sun", "li", "liang"]
Function lookup () {firstName =/\ w +/I (); if (! FirstName)
Window. alert (RegExp. input + "illegal input"); else {count = 0;
For (I = 0; I enter your surname and press Enter.
<FORM> <input type: "TEXT" NAME = "FirstName" onChange = "lookup (this);"> </FORM>
</HTML> whether the "g" mark is used in the global attribute regular expression. RegExp attribute, read-only
In Javascript 1.2, NES 3.0 and later versions provide the description that global is an attribute of an object of a unique regular expression.
If the "g" mark is used, the global value is true; otherwise, the global value is false. The "g" Mark specifies a regular expression to test all possible matches.
You cannot directly change the value of this attribute, but you can call the compile method to change it. IgnoreCase check whether the regular expression uses the "I" flag
The RegExp attribute is read-only in Javascript 1.2, and the NES 3.0 and later versions provide the description:
IgnoreCase is an attribute of an individual regular expression object.
If the "I" flag is used, true is returned; otherwise, false is returned. The "I" mark indicates that case sensitivity is ignored during matching.
You cannot directly change the value of this attribute, but you can call the compile method to change its input and point out that the regular expression needs to test the string. $ _ Is another name of this attribute.
The RegExp attribute is static in Javascript 1.2 and provided by NES 3.0 and later versions.
Description: Because input is static, it is not an attribute of an individual regular expression object. You can also use RegExp. input.
If the exec or test method of the regular expression is not provided with a string and RegExp. input has a value, its value is used to call this method.
The input attribute can be preset by a script or browser. If the value is preset and the exec or test method is called, the string is not provided.
The input value is used when exec or test is called. Input can be set in the following way in the browser:
When the text form field processing handle is called, the input is set to the string of the text input.
When the textarea form field processing handle is called, the input is set to the input string in the textarea field. Note multili
Ne is also set to true to match multiple lines of text. When the select form field processing handle is called, the input is set to the value of selected text.
When the processing handle of the linked object is called, the input is set to A string between <a href =...> and </A>.
After the event processing is completed, the value of the input attribute is cleared. LastIndex is a readable/writable integer attribute that specifies the start point of the next match.
RegExp attributes are provided in Javascript 1.2 and NES 3.0 and later versions.
Description: lastIndex is the attribute of an individual regular expression object. This attribute is set only when the "g" mark of the regular expression is used for full string matching. Follow these rules:
If the length of a large string of lastindexs, regexp.testand regexp.exe c fail, and the lastIndex is set to 0.
If lastIndex is equal to the length of a string and the regular expression matches an empty string, the regular expression starts from the position of lastIndex.
If lastIndex is equal to the length of the string and the regular expression does not match an empty string, the regular expression does not match the input and the lastIndex is set to 0.
Otherwise, lastIndex is set to the next point of the last match. For example, execute the script in the following order: re =/(hi )? /G match Null String
Re ("hi") Returns ["hi", "hi"], and sets lastIndex to 2.
Re ("hi") Returns [""], an empty array. The element with its subscript 0 is a matching string. In this case, null is returned.
The string is because the lastIndex is equal to 2 (and still 2), and the length of "hi" is also 2. LastMatch matches the string for the last time. $ & means the same.
The RegExp attribute is static and read-only in Javascript 1.2 and NES 3.0 and later versions.
Description: lastMatch is static, so it is not an attribute of a specified regular expression. You can also use RegExp. lastMatch. LastParen
The last matching string with parentheses, if any. $ + Means the same. RegExp attribute, static, read-only
Available in Javascript 1.2, NES 3.0 and later versions
Description: lastParen is static. It is not a regular expression attribute. You can use RegExp. lastParen to express the same meaning.
The last time leftContext matches the previous substring, $ 'has the same meaning. RegExp attribute, static, read-only
Available in Javascript 1.2, NES 3.0 and later versions
Description: Because leftContext is static and is not an attribute of a regular expression, RegExp. leftContext can be used to express the same meaning.
Multiline indicates whether multiple lines of text are matched. $ * means the same. RegExp attribute, static
Available in Javascript 1.2, NES 3.0 and later versions
Description: Because multiline is static, rather than the attribute of an individual regular expression, RegExp. multiline can be used to express the same meaning.
If multiple lines of text are allowed to be matched, multiline is true. If the search must be stopped in another row, the multiline is false.
The script or browser can set the multiline attribute. When a textarea event processing handle is called, multiline
Set to true. After the event processing handle is processed, the multiline attribute value is cleared. That is to say, if you set multili
If ne is true, multiline is set to false after any event processing handle is executed. Prototype
The prototype of the class. You can use prototype to add class attributes or methods as required. To obtain prototypes Resources
Expected. See the function. prototype. Property Attribute of RegExp. Starting from Javascript 1.1 and NES 2.0
ECMA version ECMA-262 rightContext last matched to the right string, $ 'is the same effect.
RegExp attribute, static, read-only from Javascript 1.2, NES 3.0 and later versions
Description: Because the rightContext is static and is not an attribute of an individual regular expression worker, RegExp. rightContext can be used to achieve the same effect.
Source is a read-only attribute that contains the pattern defined by a regular expression. It does not include forward slashes, g, or I tags. RegExp attribute, read-only
Provided since Javascript 1.2 and NES 3.0 and later versions
Description: source is the attribute of an individual regular expression object. You cannot directly change its value, but you can change it by calling the compile method. Test
Returns true or false if the regular expression of the specified string matches the search result. RegExp Method
The syntax of regexp. test ([str]) is provided starting with Javascript 1.2 and NES 3.0 and later versions.
Parameter: regexp, the name of the regular expression. It can be a variable name or a regular expression that defines a text string.
Str, the string to be matched. If omitted, the value of RegExp. input is used as the parameter.
Description: when you need to know whether a String can match a regular expression, you can use the test method (
Method). To get more information (but the speed will slow down), you can use the exec method (similar to the String. match Method ). Example: The following example shows whether the test is successful:
Function testinput (re, str ){
If (re. test (str) midstring = "contains ";
Else midstring = "does not contain ";
Document. write (str + midstring + re. source);} toSource
Returns the RegExp method of the source code of a string symbolic object. The syntax of toSource () is provided starting from Javascript 1.3 or later ()
Parameter: Not described: The toSource method returns the following values: For the built-in RegExp object, toSource returns the following symbolic source code unavailable:
Function Boolean () {[native code]}
In RegExp, toSource returns a string that symbolizes the source code. Generally, this method is automatically called by Javascript instead of explicitly called in the code.
For more information, see Object. toSource toString. RegExp Method
Starting with Javascript 1.1, NES 2.0 provides the ECMA version ECMA-262 Syntax: toString () parameter: None
Description: The RegExp Object does not consider the toString method of the Object. It does not inherit Object. toString.
The toString method returns a string representing the object. For example, the following example shows the string representing the RegExp object.
MyExp = new RegExp ("a + B + c"); alert (myExp. toString ())
For more information about displays "/a + B + c/", see: Object. toString valueOf returns the original value of a RegExp Object.
The RegExp method provides ECMA version from Javascript 1.1: ECMA-262 Syntax: valueOf ()
Parameter: no description: the valueOf method of RegExp returns the original value of the RegExp object in string form, which is equal to RegExp. toString.
This method is usually automatically called by Javascript instead of explicitly. Example: myExp = new RegExp ("a + B + c ");
Alert (myExp. valueOf () displays "/a + B + c /"

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.