Regular-expression Learning

Source: Internet
Author: User
Tags format array definition empty expression variable tostring versions
Regular

Regular expression is regular expression, it seems that English is better than Chinese understand more, is to check expression characters do not conform to the rules!! Regular expressions have a very powerful and complex object regexp, provided in the Javascript1.2 version.

Let's look at an introduction to regular expressions:

Regular expression objects are used to standardize an expression of a specification (that is, an expression character that does not meet a specific requirement, such as an email address format, etc.), and it has properties and methods for checking whether the given string conforms to the rules. In addition, the properties of the individual regular expression objects that you set up with the RegExp constructor have predefined static properties of regular expression objects that you can use at any time.

Core objects:
In JavaScript 1.2, NES more than 3.0 versions are available.
After the JavaScript 1.3 version added the Tosource method.

To establish a method:

Text formatting or regexp constructor functions.
Text formatting uses the following format:
/pattern/flags/Mode/tag

The constructor function method uses the following methods:
New RegExp ("pattern" [, "Flags"]) is new REGEXP ("mode" [, "tag"])
Parameters:
Pattern (Mode)
Text that represents the regular expression

Flags (tags)
If you specify this item, flags can be one of the following denominations:
G:global match (full set matching)
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 constructor function parameters are marked with quotation marks. So the following
Expression to create 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 (adding leading characters 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 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 a literal meaning that the character followed by a special character is not interpreted.
For example:/b/matches the character ' B ', which becomes a special character by adding a backslash, or/\b/, in front of B, which means
Match the dividing line of a word.
Or:
For a few characters, usually the description is special, indicating that the character immediately followed is 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 matches must be at the front.
For example:/^a/does not match the ' a ' in ' an 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 characters of the preceding 0 or n times.
For example:/bo*/matches ' B ' in ' boooo ' or ' a bird warbled ' in ' a ghost booooed ', but does not match ' a goat g
runted any character in the ".

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

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

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


Character (x)
Meaning: Matches ' X ' and records the matching value.
For example:/(foo)/Match and record "Foo bar." In the ' foo '. The matching substring can be in the result array of the element [1], ..., [n] Return
Back, or the attributes of the RegExp object are $, ..., the $ return.

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: Here The n is a positive integer. Matches the preceding n characters.
For example:/a{2}/does not match "a" in "Candy," but matches "Caandy," all ' a ' and "Caaandy." Two in front of
' A '.

Character {n,}
Meaning: Here The n 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: Here the N and M are all positive integers. Matches at least n up to m preceding characters.
For example:/a{1,3}/does not match any of the characters in "Cndy", but matches the first two in "a", "Caandy," in "Candy,"
"A" and "Caaaaaaandy" in front of the three ' a ', note: even if "caaaaaaandy" there are many ' a ', but only match the previous three
A ' a ' or ' AAA '.

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

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

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

Character \b
Meaning: A line that matches a word, such as a space (not confused with [\b])
For example:/\bn\w/matches ' no ' in ' Noonday ',/\wy\b/matches ' possibly yesterday. ' In the ' ly '.

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

Character \cx
Meaning: Here The 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: Matches 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-numeric, equivalent to [^0-9].
For example:/\d/or/[^0-9]/matches "B2 is the" suite number. In the ' B '.

Character \f
Meaning: matches a form character

Characters \ n
Meaning: matches a newline character

Character \ r
Meaning: matches a carriage return character

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

Character \s
Meaning: matches a single character except white spaces, 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 and underscores, equivalent to [a-za-z0-9_].
For example:/\w/matches ' 5 ' and ' 3D ' in ' a ', ' $5.28, ' in ' Apple, '. 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 '% '.

Characters \ n
Meaning: Here The n 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/match "Apple, orange, cherry, peach." In the ' Apple, Orange ', below
There is a more complete example.
Note: If the number in the left parenthesis is smaller than the number specified in \ n, then \ n takes a row of octal escape as a description.

Character \ooctal and \xhex
Meaning: Here the \ooctal is a octal escape value, and \xhex is a hexadecimal escape value that allows for a
Embed the ASCII code in the regular expression.


When an expression is checked, a literal symbol provides a way to edit the regular expression. Using literal symbols to make regular representations
The type remains 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
Compile repeatedly.
The regular Expression object builder, for example, new RegExp ("Ab+c"), provides run-time compilation of regular expressions. When you know that you are
When the pattern of expressions changes, you should use constructors, or you don't know the pattern of regular expressions, and they are from the other
When the source is obtained, such as when entered by the user. Once you have defined the regular expression, the regular expression can be used anywhere,
And can be changed, 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 line Cheng
Line to get your own RegExp object. Because each script is not interrupted in one thread, this ensures that different scripts do not override
The value of the cover 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, while 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. Don't consider Object.tos
Ource 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 global REGEXP
The value of the $ and $ property of the 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
Using the value of regexp.input as its argument, note 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" ></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 use Regexp.input to access the
Property.

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 visit 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 used in this way, do not have to pre
Consider the RegExp object first. 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
The value of the $ and $ property of the RegExp 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.

<<<<<<< Regular Expressions Detailing >>>>>>>

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" >file://HorseWith a b followed by one or more d, and then a B
file://suddenlySlightly 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" ></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.