JS Regular Expressions Daquan (3)

Source: Internet
Author: User

Regular Expressions Regular Expression detail (i)

Regular expressions are regular expression, it seems that English is better than Chinese to understand more, is to check the expression character
Not meet the rules!! Regular expressions have a very powerful and complex object RegExp, which is provided above the JavaScript1.2 version.

Let's look at an introduction to regular expressions:
The regular expression object is used to standardize the expression of a specification (that is, the expression does not meet certain requirements, such as the email address format, etc.), and it has the properties and methods used to check whether the given string conforms to the rule.

In addition to the properties of individual regular expression objects that you build with the RegExp constructor, you have pre-defined the static properties of regular expression objects, and you can use them at any time.

Core objects:
Available in JavaScript 1.2, NES 3.0 or later.
The Tosource method was added after the JavaScript 1.3 version.

Method of Establishment:
Text format or RegExp constructor function.
The text is formatted using the following format:
/pattern/flags =/mode/marker

constructor function methods use the following methods:
New RegExp ("pattern" [, "Flags"]) is new REGEXP ("pattern" [, "marker"])

Parameters:
Pattern (Mode)
Text that represents the regular expression

Flags (Mark)
If this item is specified, flags can be one of the following values:
G:global match (fully set match)
I:ignore case (ignoring capitalization)
Gi:both Global Match and ignore case (matches all possible values, also ignores capitalization)

Note: The parameters in the text format do not use quotation marks, and the parameters of the constructor function are marked with quotation marks. So the following expression establishes the same regular expression:
/ab+c/i
New RegExp ("Ab+c", "I")

Describe:
When using constructors, it is necessary to use normal string avoidance rules (the inclusion of a leading character in a string \).
For example, the following two statements are equivalent:
Re = new RegExp ("\\w+")
Re =/\w+/

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

Table 1.3: Special characters in regular expressions:

Characters
Meaning: For a character, it is usually meant to be literal, indicating that the following character is a special character, \ not interpreted.
For example:/b/matches the character ' B ', by adding a backslash in front of B, or/\b/, the character becomes a special character that represents the dividing line of a word.

Or:
For several characters, the usual description is special, which indicates that the characters immediately followed are not special, but should be interpreted literally.
For example: * is a special character that matches any character (including 0 characters); For example:/a*/means matching 0 or more aces.
To match the literal *, precede a with a backslash; For example:/a\*/matches ' A * '.

Character ^
Meaning: The character that represents the match must be at the front.
For example:/^a/does not match "an A," in the ' a ', but matches "an A." The "A" in the front.

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

Characters
Meaning: matches the preceding character 0 or n times.
For example:/bo*/matches ' B ' in ' boooo ' or ' a bird warbled ' in ' a ghost booooed ', but does not match any of the characters in ' a goat grunted '.

Character +
Meaning: Matches the character preceding the + sign 1 or n times. Equivalent to {1,}.
For example:/a+/matches ' a ' and ' Caaaaaaandy ' in ' Candy '. All ' a ' in the.

Character?
Meaning: match the preceding character 0 or 1 times.
For example:/e?le?/matches ' el ' and ' angle ' in ' Angel '. In the ' Le '.

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

Character (x)
Meaning: Match ' x ' and record the matching value.
For example:/(foo)/Match and record "Foo bar." In the ' foo '. The matched substring can be returned by the attribute in the result array [1], ..., [n], or by the property of the RegExp object $ ..., $9.

Character X|y
Meaning: Match ' x ' or ' Y '.
For example:/green|red/matches ' green ' and ' Red apple ' in ' green apple '. In the ' Red '.

Character {n}
Meaning: n Here is a positive integer. Matches the preceding n characters.
For example:/a{2}/does not match ' a ' in ' Candy, ' but matches all ' a ' and ' Caaandy ' in ' Caandy, '. In front of the two
' A '.

Character {n,}
Meaning: n Here is a positive integer. Matches at least n preceding characters.
For example:/a{2,} does not match ' a ' in ' Candy ', but matches all ' a ' and ' Caaaaaaandy ' in ' Caandy '. All ' a ' in the

Character {n,m}
Meaning: Both N and m are positive integers. Matches at least N of the characters up to M preceding.
For example:/a{1,3}/does not match any characters in "Cndy", but matches "candy," in the "a", "Caandy," in front of two
' A ' and ' Caaaaaaandy ' in front of the three ' a ', note: even if "Caaaaaaandy" has a lot of ' a ', but only match the previous three ' a ' is "AAA".

character [XYZ]
Meaning: A list of characters that matches any one of the characters listed. You can use hyphens-to indicate a range of characters.
For example: [ABCD] is the same as [a-c]. They match ' C ' in ' B ' and ' ache ' in ' brisket '.

character [^XYZ]
Meaning: A character complement, that is, it matches everything except the listed characters. You can use hyphens-to indicate a range of characters.
For example: [^ABC] and [^a-c] are equivalent, they first match ' R ' and ' chop ' in ' brisket '. In the ' H '.

character [\b]
Meaning: Match a space (do not confuse with \b)

Character \b
Meaning: Match the dividing line of a word, such as a space (do not confuse [\b])
For example:/\bn\w/matches ' no ' in ' Noonday ',/\wy\b/matches ' possibly yesterday. ' In the ' ly '.

Character \b
Meaning: Match the non-dividing line of a word
For example:/\w\bn/matches ' on ' in "Noonday",/y\b\w/matches "possibly yesterday." In the ' ye '.

Character \cx
Meaning: The x here is a control character. Matches the control character of a string.
For example:/\cm/matches a control-m in a string.

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

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

Character \f
Meaning: Match a single form character

Character \ n
Meaning: Match a line break

Character \ r
Meaning: Match a carriage return character

Character \s
Meaning: Match a single white space character, including spaces, tab,form feeds, line breaks, equivalent to [\f\n\r\t\v].
For example:/\s\w*/matches "foo bar." In the ' bar '.

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

Character \ t
Meaning: Match a tab

Character \v
Meaning: Match a head tab

Character \w
Meaning: Matches all numbers and letters as well as underscores, equivalent to [a-za-z0-9_].
For example:/\w/matches "Apple," in ' a ', ' $5.28, ' in ' 5 ' and ' 3D. ' In the ' 3 '.

Character \w
Meaning: Matches other characters except numbers, letters, and underscores, equivalent to [^a-za-z0-9_].
For example:/\w/or/[^ $A-za-z0-9_]/match "50%." In the '% '.

Character \ n
Meaning: n Here is a positive integer. Matches the value of N of the last substring of a regular expression (counting the left parenthesis).

For example:/apple (,) \sorange\1/matches "Apple, orange, cherry, peach." In the ' Apple, Orange ', here's a more complete example.
Note: If the number in the left parenthesis is smaller than the number specified in \ n, a line of octal escape is removed as a description.

Characters \ooctal and \xhex
Meaning: The \ooctal here is an octal escape value, while \xhex is a hexadecimal escape value that allows the embedding of ASCII code in a regular expression.


When an expression is checked, the text symbol provides a way to edit the regular expression. The use of literal notation allows the regular expression to remain constant. For example, if you use a literal symbol in a loop to construct a regular expression, the regular expression does not need to be compiled repeatedly.

The regular Expression object constructor, for example, new RegExp ("Ab+c"), provides run-time compilation of regular expressions. When you know that the pattern of regular expressions changes, you should use constructors, or you don't know the pattern of regular expressions, and they are obtained from another source, such as when the user enters. 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 pre-defined RegExp object can be used in each window, that is, each detached JavaScript thread runs to obtain its own RegExp object. Because each script is non-interruptible within a 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 $9. Input and Multiline properties are preset. The values of other static properties are set after the exec and test methods of the individual regular expression objects are executed, and after the match and replace methods of the string are executed.

Property
Note that several properties of the RegExp object are both long names and short names (like Perl). These names are all pointing to the same value. Perl is
A programming language, and JavaScript mimics its regular expression.

Properties $, ..., $9
Get a matching substring, if any

Property $_
Reference input

Property $*
Reference Multiline

Property $&
Reference Lastmatch

Property $+
Reference Lastparen

Property $ '
Reference Leftcontext

Property $ '
Reference Rightcontext

Property constructor
Specifies the object to use to create the prototype letter

Property Global
Determines whether the regular expression does not match all strings, or only conflicts with the first.

Property ignorecase
Determines whether the case is ignored when trying to match a string

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

Property lastindex
Decide where the next match starts.

Property Lastmatch
The last matching character

Property Lastparen
When the substring matches, the last parenthesized, if any.

Property Leftcontext
The substring before the most recent match.

Property Multiline
Whether to search in multiple lines of the string.

Property prototype
Allow attached properties to all objects

Property Rightcontext
The substring that was last matched.

Property source
Pattern text

Method
Compile method
Compiling a regular Expression object

exec method
Run regular expression matching

Test method
Test regular up-to-match

Tosource method
Returns the text of an object describing the specified object; You can use this value to create a new object. Do not consider 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 a word in a string. In the replaced text, the script uses the values of the $ and $ properties of the global RegExp object. Notice that the name of the $ property of the RegExp object is passed to the Replace method as the second argument.
<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 is set by the change event handling handle. In the GetInfo function, the Exec method
Using the value of regexp.input as its argument, note that the $ property 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 surname and age, and press ENTER when you are finished.
<form><input type= "TEXT" name= "Nameage" onchange= "getInfo (this);" ></FORM>
</HTML>


$ $, ..., $9 property
A matching substring enclosed in parentheses, if any.
is the attribute of the RegExp
Static, read-only

In JavaScript 1.2, NES 3.0 or later 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 parenthesized is unrestricted, but the regular expression object retains only the last 9 bars. If you want to access all the matching strings within the parentheses, you can use the returned array.

These properties can be used in the string after the Regexp.replace method is replaced (output result). When you use this method, you don't have to think about regexp objects beforehand. An example is given below. When no parentheses are included in the regular expression, the script interprets the literal meaning of $n. (n here 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 a regular expression
The value of the $ and $ properties of the RegExp object. Note: When they pass arguments to the Replace method, there is no consideration for the $ property
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 shown is: Smith, John.


Regular Expressions Regular Expression detail (ii)

Regular Expressions in detail (ii)

The following are new objects that are not regular expressions see the properties of the corresponding JavaScript object $_ Properties reference the input $* property

Reference Multiline $& Property Reference Lastmatch $+ Property Reference Lastparen $ ' property

Refer to the Leftcontext $ ' property Reference Rightcontext compile method compiles the regular expression object   while the script is running;
The RegExp method in JavaScript 1.2, NES 3.0 or later provides syntax:  
Regexp.compile (pattern[, Flags]) Number: The name of the regexp regular expression, which can be either a variable name or a literal string.   The definition text of the
pattern regular expression. Flags, if specified, can be one of the following: "G": matches all possible string  
"I": Ignores Case "GI": matches all possible strings and ignores case description:  
Use the Compile method to compile a regular expression Created with the RegExp constructor function. This   the
force the regular expression to compile only once, rather than compiling it every time a regular expression is encountered. When you confirm that the regular expression can be  , and the
remains constant, you can use the compile method to compile it (after obtaining its matching pattern) so that it can be reused multiple times in the script.  
You can also use the Compile method to change the regular expression during run time. For example, if the regular expression changes,  
You can use the compile method to recompile the object to improve efficiency.  
Use this method to change the value of the source, global, and Ignorecasesource properties of the regular expression. constructor 
indicates the function that establishes the object's prototype. Note that the value of this property is provided by the function itself, not by a string containing regexp name.property.  
in JavaScript 1.1, NES version 2.0 is available in the version ECMA-262 Description: reference object.constructor. 
The Exec method runs a matching search on the specified string. Returns an array of results. Is the method of RegExp  
in JavaScript 1.2, NES version 3.0 provides syntax: Regexp.exec ([str]) regexp ([str])

Parameter: regexp, the name of the regular expression, can be a variable name or a literal definition string.
STR, the string to match the regular expression, if omitted, will use the value of Regexp.input.

Description: As in the syntax description, the Exec method of the regular expression can be called directly (using Regexp.exec (str)) or indirectly (using RegExp (str)).
If you are just running to find out if it matches, you can use the string search method.
If the match succeeds, the Exec method returns an array and updates the value of the regular Expression object property and the pre-defined regular expression object, RegExp. If the match fails, the Exec method returns NULL.

Consider the following example: <script language= "JavaScript1.2" >//Match one B followed by one or more d, followed by a B
Ignoring case myre=/d (b +) (d)/ig; MyArray = Myre.exec ("Cdbbdbsbz");
</SCRIPT> The following is the return value of the script: Object Properties/index Description Example
MyArray

MyArray content ["DBBD", "BB", "D"]
Index
Based on 0 Match index 1
Input
Raw String CDBBDBSBZ
[0]
The last matching 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
The index value of the next match operation starts at 5
IgnoreCase
Indicates whether "I" is used to ignore case true
Global
Indicates whether to use the "G" tag to match all possible strings true
Source
Text string defining pattern D (b +) (d)
Regexp
lastmatch$&
The last matching character dbbd
Leftcontext$\q
Latest match preceding substring C
rightcontext$ '
The last substring after the match bsbz
$, ... $9
The matching substring within the parentheses, if any. There is no limit to the number of parentheses, but RegExp can only retain the last 9 = BB
$ = d
Lastparen $+
The last matched 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 that is 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 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 last name of the registered user has been stored in array a, perhaps 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>
</HTML>

The "G" tag is used in the global property regular expression. RegExp property, read-only  
in JavaScript 1.2, NES 3.0 or later 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; False The "G" tag specifies that the regular expression tests all possible matches.  
You cannot directly change the value of the property, but you can call the compile method to change it. IgnoreCase checks if the regular expression uses the "I" tag  
RegExp property, read-only in JavaScript 1.2, NES 3.0 or later provides a description:  
IgnoreCase is a property of an individual regular expression object.  
Returns True if the "I" token is used, otherwise false is returned. The "I" flag indicates that the case is ignored when the match is made.  
You cannot directly change the value of the property, but you can change it by calling the compile method, input indicates that the string is to be tested by the regular expression. $_ is another name for this property.  
RegExp properties, static in JavaScript 1.2, NES 3.0 or later available

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 no string is supplied to the exec or test method of the regular expression, and there is a value in Regexp.input, the method is invoked using its value.
The script or browser can preset the input property. If a value is preset and the Exec or test method is called without a string
The value of input is used when exec or test is called. Input can be set by the browser in the following way:
When the text form field processing handle is called, input is set to the text input string.
When the textarea form field processing handle is called, input is set to the string entered 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, input is set to the value of selected text.
When the handle of the linked object is called, input is set to the string between <a href=...> and </A>.
The value of the input property is cleared after the event management handle has been processed. LastIndex a readable/writable integer attribute that indicates where the next match will start.
RegExp properties are available in JavaScript 1.2, NES 3.0 or later

Description: LastIndex is a property of an individual regular expression object. This property is set only when the "G" token of the regular expression is used for a full string match. The following rules apply:

If the length of the lastindex size string, regexp.test and Regexp.exec fail, and lastindex is set to 0.

If lastindex equals the length of the string and the regular expression matches the empty string, the regular expression begins to match the position of the lastindex.

If lastindex equals the length of the string and the regular expression does not match the empty string, the regular expression does not match input, and 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 matches an empty string
Re ("HI") returns ["Hi", "Hi"],lastindex 2
Re ("HI") returns [""], an empty array whose subscript 0 element is the matching string. In this case, the return null
The string is because lastindex equals 2 (and is still 2), and the length of "HI" is also 2. Lastmatch last match string,$& is the same meaning.
RegExp properties, static, read-only in JavaScript 1.2, NES 3.0 or later available

Description: Because lastmatch is static, it is not an individual attribute of the specified regular expression. You can also use Regexp.lastmatch. Lastparen
The last time a matching string with parentheses is added, if any. $+ is the same meaning. RegExp property, Static, read-only
In JavaScript 1.2, NES 3.0 or later offers

Description: Because Lastparen is static, it is not an individual regular property, and you can use Regexp.lastparen to express the same meaning.
Leftcontext the last string that matches the preceding substring, $ ' has the same meaning. RegExp properties, static, read-only
In JavaScript 1.2, NES 3.0 or later offers

Description: Because Leftcontext is static and is not a property of a regular expression, you can use Regexp.leftcontext to express what you want to mean.
Multiline reflects whether multiple lines of text are matched, $* is the same meaning. RegExp Properties, static
In JavaScript 1.2, NES 3.0 or later offers

Description: Because multiline is static and not a property of an individual regular expression, you can use Regexp.multiline to express the same meaning.
True if multiple lines of text are allowed, false if the search must stop at multiline.
The script or browser can set the Multiline property. When a textarea event handle is called, multiline
is set to true. The Multiline property value is cleared after the event handling handle has finished processing. That is, if you set multiline to True, multiline is reset to false after any event-handling handle is executed. Prototype
Depicts the prototype of a class. You can use prototype as required to add properties or methods to a class. For information on prototypes, see the Function.prototype.Property property of RegExp. From JavaScript 1.1, NES version 2.0 is available
ECMA version ECMA-262 rightcontext the last match to the right of the string, $ ' is the same effect.
RegExp properties, static, read-only from JavaScript 1.2, NES 3.0 or later available

Description: Because Rightcontext is static and is not an attribute of an individual regular expression, you can use Regexp.rightcontext to achieve the same effect.
Source A read-only property that contains the pattern defined by the regular expression, not including the forward slashes and the "G" or "I" tags. RegExp properties, read-only
From JavaScript 1.2, NES more than 3.0 versions are available

Description: Source is a property of an individual regular expression object, and you cannot change its value directly, but you can change it by calling the compile method. Test
Executes the regular expression of the specified string to match the search, returning True or false. Methods of RegExp
From JavaScript 1.2, NES 3.0 or later provides syntax: Regexp.test ([str])

Parameter: regexp, the name of the regular expression, which can be either a variable name or a regular expression definition 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, you can use the test method (with the String.search side
For more information (but slower speed), you can use the Exec method (similar to the String.match method). Example: The following example shows the prompt for the success of test:

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 or later: Tosource ()

Parameter: No Description: The Tosource method returns the following value: For the built-in RegExp object, Tosource returns the characters below to symbolize that the source code is not available:
function Boolean () {[native code]}
In a regexp scenario, Tosource returns a string that symbolizes the source code, which is typically called automatically by JavaScript internally rather than explicitly in a non-coding way.
See Object.tosource ToString Returns a string depicting the specified object. Methods of RegExp
From JavaScript 1.1, NES 2.0 begins with the ECMA version ECMA-262 syntax: toString () Parameter: None

Description: The RegExp object does not account for 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 symbolizing the RegExp object
Myexp = new RegExp ("A+b+c"); Alert (myexp.tostring ())
Displays "/a+b+c/" See more: Object.ToString valueOf returns the original value of a RegExp object
The RegExp method provides the ECMA version from the version of JavaScript 1.1: ECMA-262 Syntax: ValueOf ()

Parameter: 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 called automatically by JavaScript internally instead of explicitly invoking an example: Myexp = new RegExp ("A+b+c");
Alert (myexp.valueof ()) displays "/a+b+c/"

JS Regular Expressions Daquan (3)

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.