Regular expression (2)

Source: Internet
Author: User

Regular Expression details (2)

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"> // match a B, followed by one or more d, followed by a B
// Ignore 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.