When an expression is checked, a literal symbol provides a way to edit the regular expression. You can use a literal symbol to keep the regular expression constant. For example, if you use a literal symbol to construct a regular expression in a loop, the regular expression does not need to be compiled repeatedly. The regular Expression object builder, for example, new RegExp ("Ab+c"), provides run-time compilation of regular expressions. When you know that the patterns of regular expressions change, you should use constructors, or you don't know the pattern of regular expressions, when they are obtained from another source, such as when entered by a user. Once you have defined the regular expression, the regular expression can be used anywhere and can be changed, and you can use the compile method to compile a new regular expression for reuse.
A separate predefined RegExp object can be used in each window, that is, each detached JavaScript thread runs to get its own RegExp object. Because each script is non-disruptive in one thread, this ensures that different scripts do not overwrite the value of the RegExp object. The predefined RegExp objects contain static properties: Input, Multiline, Lastmatch,lastparen, Leftcontext,rightcontext, and from $ to $. Input and Multiline properties are preset. The values of other static properties are in the execution of individual regular
After the exec and test methods of an expression object, and after the match and replace methods of the string are executed.
Property
Note that several properties of the RegExp object have both long and short names (like Perl). These names all point to the same value. Perl is a programming language, and JavaScript imitates its regular expression.
Properties $, ..., $
Gets a matching substring, if any.
Attribute $_
Reference input
Attribute $*
Reference Multiline
Attribute $&
Reference Lastmatch
Attribute $+
Reference Lastparen
Attribute $ '
Reference Leftcontext
Attribute $ '
Reference Rightcontext
Attribute constructor
Specify the object to be used to create the prototype letter
Attribute Global
Determines whether to test whether a regular expression cannot match all strings, or just the first conflict.
Attribute ignorecase
Determines whether case is ignored when trying to match a string
Attribute input
When the regular expression is matched, the string is the opposite.
Attribute lastindex
Decide where the next match starts
Attribute Lastmatch
Last matching character
Attribute Lastparen
SUBSTRING matches when the last parenthesized, if any.
Attribute Leftcontext
Substring before the most recent match.
Attribute Multiline
Whether to search in multiple lines of a string.
Attribute prototype
Allow attributes to be attached to all objects
Attribute Rightcontext
Substring of the most recent match.
Property source
Modal text
Method
Compile method
Compiling a regular Expression object
exec method
Run regular expression matching
Test method
Test regular up-match
Tosource method
Returns the object specified by the text description of an object; You can use this value to create a new object. Do not consider the Object.tosource method.
ToString Method
Returns a string describing the specified object, regardless of the Object.ToString object.
ValueOf method
Returns the original value of the specified diagonal. Do not consider the Object.valueof method.
In addition, this object inherits the watch and Unwatch methods of the object
Example:
Example 1, the following example script uses the Replace method to convert words in a string. In the replaced text, the script uses the value of the $ and $ property of the global RegExp object. Note that when passed to the Replace method as the second argument, the name of the $ property of the RegExp object
Said.
<script language= "Javascript1.2" >
Re =/(\w+) \s (\w+)/;
str = "John Smith";
Newstr=str.replace (Re, "$ $");
document.write (NEWSTR)
</script>
Show results: "Smith, John".
Example 2, the following example script, regexp.input by the change event handles the handle setting. In the GetInfo function, the Exec method uses the value of regexp.input as its argument, noting that the $ attribute is regexp preset.
<script language= "Javascript1.2" >
function GetInfo (ABC)
{
Re =/(\w+) \s (\d+)/;
Re.exec (Abc.value);
Window.alert (regexp.$1 + ", your is" + regexp.$2);
}
</script>
Please enter your last name and age and press ENTER when you are finished.
<form><input type= "TEXT" name= "Nameage" onchange= "getInfo (this);" ></FORM>
</HTML>
$, ..., $ property
A matching substring enclosed in parentheses, if any.
Is the property of the RegExp
Static, read-only
In JavaScript 1.2, NES version 3.0 offers
Description: Because input is a static property, it is not a property of an individual regular expression object. You can access this property using Regexp.input.
The number of substrings that can be prefixed with parentheses is unrestricted, but the regular expression object can only retain the last 9 bars. If you want to access all the parentheses inside the matching string, you can use the returned array.
These properties can be replaced by the Regexp.replace method (output). When you use this method, you do not have to consider the RegExp object beforehand. An example is given below. When the regular expression does not contain parentheses, the script interprets the literal meaning of $n
Yi. (Here's n is a positive integer).
For example:
The following example script uses the Replace method to swap the position of a word in a string. In the replaced text string, the script uses the regular expression to regexp the value of the $ and $ property of the object. Note: When they pass parameters to the Replace method, there is no consideration for the $ attribute
The name of the RegExp object.
<script language= "Javascript1.2" >
Re =/(\w+) \s (\w+)/;
str = "John Smith";
Newstr=str.replace (Re, "$ $");
document.write (NEWSTR)
</script>
The output displayed is: Smith, John.
U/meil/archives/2007/324.html
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.