Regular expression Regular expression details (ii) _ Regular expressions

Source: Internet
Author: User
Tags regular expression
Regular Expressions in detail (II.)

The following are new objects that are not regular expressions see the corresponding JavaScript object's Properties $_ Property Reference Input $* property
Reference Multiline $& Property Reference Lastmatch $+ Property Reference Lastparen $ ' property
Reference Leftcontext $ ' Property Reference Rightcontext compile method to compile regular expression objects during script run
Methods belonging to RegExp provide syntax in JavaScript 1.2, NES over 3.0 versions:
Regexp.compile (pattern[, flags]) in number: RegExp the name of a regular expression, either a variable name or a string of text.
The definition text of the pattern regular expression. Flags, if specified, can be one of the following: "G": match all possible strings
"I": Ignores Case "GI": matches all possible strings and ignores case descriptions:
Use the Compile method to compile a regular expression created with the RegExp constructor function. Such
Forces the regular expression to compile only once, not every time a regular expression is encountered. When you confirm that the regular expression is capable of
You can use the compile method to compile it (after you get its matching pattern) so that it is repeated multiple times in your script.
You can also use the Compile method to change the regular expression during run time. For example, if the regular expression is changed,
You can use the compile method to recompile the object to improve efficiency.
Using this method will change the value of the source, global, and Ignorecasesource properties of the regular expression. Constructor
Indicates the function that establishes the object prototype. Note that the value of this property is provided by the function itself, not by a string containing the RegExp name.property.
In JavaScript 1.1, NES 2.0 version provides ECMA version ECMA-262 description: Reference object.constructor.
The Exec method runs a matching search in the specified string.    Returns an array of results. It's a regexp way.
In JavaScript 1.2, NES version 3.0 provides syntax: Regexp.exec ([str]) regexp ([str])
Parameters: RegExp, the name of the regular expression, can be a variable name or a text definition string.
STR, to match the string of the regular expression, if omitted, the value of the regexp.input is used.
Description: As in the syntax description, the Exec method of a regular expression can be invoked directly (using Regexp.exec (str)) or indirectly (using RegExp (str)).
If you're just running to find out if it matches, you can use string search methods.
If the match succeeds, the Exec method returns an array and updates the value of the regular Expression object property and the predefined regular expression object, RegExp. If the match fails, the Exec method returns NULL.
Take a look at the following example: <script language= "JavaScript1.2" >//Match one B followed by one or more d, then another B
Ignores case myre=/d (b +) (d)/ig; MyArray = Myre.exec ("Cdbbdbsbz");
</SCRIPT> below is the return value of the script: Object attribute/index Description Example
MyArray

MyArray content ["DBBD", "BB", "D"]
Index
0-based matching index 1
Input
Raw String CDBBDBSBZ
[0]
Last-matched character dbbd
[1], ... N
A matching string enclosed in parentheses, if any.  Does not limit the number of parentheses. [1] = BB
[2] = d
Myre
Lastindex
Index value of starting next match operation 5
IgnoreCase
Indicates whether "I" is used to ignore case true
Global
Indicates whether the "G" tag is used to match all possible string true
Source
A text string that defines the pattern D (b +) (d)
Regexp
lastmatch$&
Last-matched character dbbd
Leftcontext$q
Latest Match substring C in front
rightcontext$ '
Substring BSBZ after the latest match
$, ...
The matching substring within the parentheses, if any. The number of parentheses is unrestricted, but RegExp can only retain the last 9 $ = BB
$ = d
Lastparen $+
The last matching substring with parentheses, if any, D

If your regular expression uses the "G" tag, you can use the Exec method multiple times to match the same string consecutively. When you do this
, the new match starts in a substring determined by the Lastindex property value of the regular expression. For example, suppose you use the following script:
<script language= "JavaScript1.2" > myre=/ab*/g;str = "Abbcdefabh"
myarray = myre.exec (str);
Document.writeln ("Found" +myarray[0]+). Next match starts at "+myre.lastindex"
Mysecondarray = myre.exec (str);
Document.writeln ("Found" +mysecondarray[0]+). Next match starts at "+myre.lastindex"
</SCRIPT> This script shows 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 a matching operation based on the input. Then check the array to see if it matches the other user's name.
This script assumes that the last name of the registered user has been stored 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 last name and press ENTER.)
<form><input TYPE: "TEXT" Name= "FirstName" onchange= "lookup (this);" ></FORM>
Whether the "G" tag is used in the </HTML> global property regular expression. RegExp property, Read only
In JavaScript 1.2, NES version 3.0 provides a description: Global is a property of an individual regular expression object
If the "G" tag is used, the value of global is true; The "G" tag specifies that the regular expression tests all possible matches.
You can't change the value of the property directly, but you can call the compile method to change it. IgnoreCase checks whether the regular expression uses the "I" tag
RegExp property, read only in JavaScript 1.2, NES 3.0 version provides description:
IgnoreCase is an attribute of an individual regular expression object.
Returns true if the "I" tag is used, otherwise returns false. The "I" tag indicates that case is ignored when a match is made.
You can't change the value of the property directly, but you can change it by calling the compile method. Input indicates that the regular expression tests that string. $_ is another name for this attribute.
RegExp properties, static in JavaScript 1.2, NES version 3.0 above provides
Description: Because input is static, it is not a property of an individual regular expression object. You can also use Regexp.input to express.
If you do not supply a string to the Exec or test method of the regular expression, and if there is a value in the Regexp.input, the method is invoked using its value.
The script or browser can preset the input property. If a value is preset and a string is not supplied when calling the Exec or test method
The value of input is used when calling exec or test. Input can be set by the browser in the following way:
When the text form field processing handle is invoked, input is set to the string of the text input.
When the textarea form field processing handle is invoked, input is set to the string entered in the TextArea field. Pay attention to Multili
NE is also set to true to match multiple lines of text. When the Select form field processing handle is invoked, input is set to the value of selected text.
When the processing handle of a linked object is invoked, input is set to a string between the <a href=...> and the </A>.
The value of the input property is cleared when the event-handler handle is finished. Lastindex a readable/writable integer property that indicates where the next match starts.
RegExp's properties are available in JavaScript 1.2, NES version 3.0 above
Description: Lastindex is the property of an individual regular expression object. This property is set only when the "G" tag of the regular expression is used to perform a full string match. The following rules apply:
If the length of the lastindex size string, regexp.test and regexp.exec fail, and the lastindex is set to 0.
If the lastindex equals the length of the string and the regular expression matches the empty string, the regular expression starts at the lastindex position.
If the lastindex equals the length of the string and the regular expression does not match the empty string, the regular expression does not match the input and the lastindex is set to 0.
Otherwise, the lastindex is set to the next point of the last match. For example, execute the script in the following order: RE =/(HI)?/g match empty string
Re ("HI") returns ["Hi", "Hi"],lastindex to 2
Re ("HI") returns ["], an empty array whose subscript 0 element is the matching string. In this case, return null
The string is because the lastindex equals 2 (and still is 2), and "HI" is also 2. Lastmatch last match string,$& is the same meaning.
RegExp properties, static, read-only in JavaScript 1.2, NES 3.0 version offers
Description: Because lastmatch is static, it is not an attribute that specifies the regular expression individually.    You can also use Regexp.lastmatch. Lastparen
The last time you enclose the matching string, if any.    $+ is the same meaning. RegExp property, Static, read-only
In JavaScript 1.2, NES version 3.0 offers
Description: Because Lastparen is static, it is not an attribute of an individual regular type, you can use Regexp.lastparen to express the same meaning.
Leftcontext last match the preceding substring, $ ' has the same meaning. RegExp properties, static, read-only
In JavaScript 1.2, NES version 3.0 offers
Description: Because leftcontext is static, not a regular expression, you can use Regexp.leftcontext to express the same meaning.
Multiline reflect whether to match multiple lines of text, $* is the same meaning. RegExp Properties, static
In JavaScript 1.2, NES version 3.0 offers
Description: Because multiline is static rather than an attribute of an individual regular expression, it is possible to express the same meaning with regexp.multiline.
True if multiple lines of text is allowed, false if the search must stop on multiline.
The script or browser can set the Multiline property. When a textarea event-handling handle is invoked, the multiline
is set to true. The Multiline property value is cleared after the event handling handle has finished processing. In other words, if you set the Multili
NE is true, multiline is set to false after any event-handling handles are executed. Prototype
Describe the prototype of the class. You can use prototype to add properties or methods to your class as required. In order to obtain prototypes's capital
, see the Function.prototype.Property property for RegExp. From JavaScript 1.1, NES version 2.0 starts offering
ECMA version ECMA-262 rightcontext the last matching string on the right, $ ' is the same effect.
RegExp properties, static, read-only from JavaScript 1.2, NES 3.0 above version started offering
Description: Because rightcontext is static, not an individual regular expression of the property, you can use Regexp.rightcontext to achieve the same effect.
Source is a read-only property that contains the pattern defined by the regular expression, not the forward slashes and "G" or "I" tags. RegExp Properties, Read only
From JavaScript 1.2, NES version 3.0 begins to provide
Description: Source is the property of an individual regular expression object, you can't change its value directly, but you can change it by calling the compile method. Test
Performs a regular-expression matching search for the specified string, returning True or false. The method of RegExp
From JavaScript 1.2, NES version 3.0 begins to provide syntax: Regexp.test ([str])
Parameters: RegExp, the name of the regular expression, either a variable name or a regular expression that defines a text string
STR, to match the string, if omitted, will use the value of regexp.input as the argument
Description: When you need to know if a string can match a regular expression, use the test method (with the String.search side
In order to obtain more information (but the speed will be slow), you can use the Exec method (similar to the String.match method). Example: The following example shows a hint that 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 a String representing the source code of the object RegExp syntax from JavaScript 1.3 above: Tosource ()
Parameter: No Description: The Tosource method returns the following value: For the built-in RegExp object, Tosource returns the following characters symbolizing the source code is not available:
function Boolean () {[native code]}
In regexp situations, Tosource returns a string of symbolic source code, which is usually invoked automatically by the JavaScript internal rather than by an explicit call.
More See Object.tosource toString returns a string depicting the specified object. The method of RegExp
From JavaScript 1.1, NES 2.0 begins to provide ECMA version ECMA-262 syntax: toString () Parameter: None
Description: The RegExp object does not consider the ToString method of object objects; it does not inherit object.tostring, for RegExp
Like, the ToString method returns a string representing the object. For example: The following example shows a string representing a RegExp object
Myexp = new RegExp ("A+b+c"); Alert (myexp.tostring ())
Displays "/a+b+c/" more see: Object.ToString valueof Returns the original value of a RegExp object
The RegExp method provides the ECMA version starting with the JavaScript 1.1 version: ECMA-262 syntax: valueof ()
Parameters: No Description: RegExp's ValueOf method returns the original value of the RegExp object as a string, which is equal to regexp.tostring.
This method is usually invoked automatically by JavaScript inside instead of an explicit invocation 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.