RegExp Regular Expression Object

Source: Internet
Author: User

JavaScript's RegExp objects are created in two ways, one literal and one object.

var r =/pattern/attributes or new REGEXP (pattern, attributes);

Parameter pattern is a string that specifies the pattern of a regular expression or other regular expression. The parameter attributes is an optional string that contains the property "G", "I", and "M", respectively, for specifying global matching, case-sensitive matching, and multiline matching. The M attribute is not supported until ECMAScript is normalized. If pattern is a regular expression, not a string, you must omit the parameter.

A new RegExp object that has the specified pattern and flags. If the parameter pattern is a regular expression instead of a string, the RegExp () constructor creates a new RegExp object with the same pattern and flags as the specified REGEXP.

Without the new operator, and REGEXP () as a function call, it behaves as if it were called with the new operator, except that when pattern is a regular expression, it only returns pattern, and no longer creates a new The RegExp object.

SyntaxError-Throws the exception if pattern is not a valid regular expression, or if the attributes contains characters other than "G", "I", and "M".

TypeError-Throws the exception if pattern is a RegExp object but does not omit the attributes parameter.

modifier Description
I Performs a match that is not case sensitive.
G Performs a global match (finds all matches rather than stops after the first match is found).
M Performs multi-line matching.

Square brackets are used to find characters in a range:

An expression Description
[ABC] Look for any characters between square brackets.
[^ABC] Look for any characters that are not in square brackets.
[0-9] Look for any number from 0 to 9.
[A-z] Finds any characters from a to lowercase z from a small write.
[A-z] Look for any characters from uppercase A to uppercase Z.
[A-z] Look for any characters from uppercase A to lowercase z.
[ADGK] Finds any character within a given collection.
[^ADGK] Finds any character outside the given collection.
(Red|blue|green) Finds any of the specified options.

Metacharacters (metacharacter) is a character that has a special meaning:

Meta character Description
. Finds a single character, in addition to line breaks and line terminators.
\w Find the word character.
\w Finds non-word characters.
\d Find numbers.
\d Finds non-numeric characters.
\s Find white space characters.
\s Finds non-whitespace characters.
\b Matches the word boundary.
\b Matches a non-word boundary.
/ Find NUL characters.
\ n Find line breaks.
\f Find a page break.
\ r Look for the carriage return character.
\ t Find tabs.
\v Find vertical tabs.
\xxx Find the characters that are specified in octal number XXX.
\xdd Finds the characters specified in hexadecimal number DD.
\uxxxx Finds the Unicode characters specified in hexadecimal number xxxx.

Quantifiers

quantifier Description
n+ Matches any string that contains at least one n.
N Matches any string that contains 0 or more N.
N? Matches any string that contains 0 or one n.
N{X} Matches a string that contains a sequence of X N.
N{x,y} Matches a string containing a sequence of X or Y N.
N{x,} Matches a string that contains at least X N of a sequence.
n$ Matches any string that ends with N.
^n Matches any string that begins with N.
? =n Matches any string immediately followed by the specified string n.
?! N Matches any string that does not follow the specified string n immediately thereafter.

RegExp Object Properties

Properties Description FF IE
Global RegExp whether the object has a flag g. 1 4
IgnoreCase RegExp whether the object has a flag I. 1 4
LastIndex An integer that indicates the character position at which to start the next match. 1 4
Multiline RegExp whether the object has a flag m. 1 4
Source The source text of the regular expression. 1 4

The LastIndex property is used to specify the starting position of the next match. This property holds an integer that declares the position of the first character after the last matching text. The results of the last match were found by methods Regexp.exec () and Regexp.test (), which are the starting point for the next retrieval, as indicated by the LastIndex property. In this way, you can iterate through all the matching text in a string by calling both methods repeatedly. This property is readable and writable. It can be set as long as the next search for the target string starts. When the method exec () or test () can no longer find text that matches, they automatically reset the LastIndex property to 0.

Important: REGEXP objects that do not have a flag G and do not represent a global schema cannot use the LastIndex property. If you start retrieving another new string after successfully matching a string, you need to manually set this property to 0. The character position starts at 1.

RegExp Object Methods

Method Description FF IE
Compile Compiles the regular expression. 1 4
Exec Retrieves the value specified in the string. Returns the found value and determines its location. 1 4
Test Retrieves the value specified in the string. Returns TRUE or FALSE. 1 4

The compile () method is used to compile regular expressions during script execution. The compile () method can also be used to change and recompile regular expressions.

Regexpobject.compile (Regexp,modifier), the first argument is a regular expression, and the second parameter is the identifier. Compiling can make execution faster.

The exec () method is used to retrieve the matching of regular expressions in a string. The parameter is a string. Returns an array that holds the results of the match. If no match is found, the return value is null.

The exec () method is powerful, is a common method, and is more complex to use than the test () method and the method that supports the String object of a regular expression.

If EXEC () finds the matched text, it returns an array of results. Otherwise, NULL is returned. The No. 0 element of this array is the text that matches the regular expression, and the 1th element is the text that matches the 1th sub-expression of regexpobject (if any), and the 2nd element is the text (if any) that matches the 2nd sub-expression of regexpobject, in such Push. In addition to the array element and the length property, the Exec () method returns two properties. The Index property declares the position of the first character of the matched text. The input property holds the string that was retrieved. We can see that when calling the Exec () method of a non-global REGEXP object, the returned array is the same as the array returned by the calling Method String.match ().

However, when Regexpobject is a global regular expression, the behavior of exec () is slightly more complex. It will begin retrieving string strings at the character specified by the LastIndex property of Regexpobject. When exec () finds text that matches the expression, it sets the Regexpobject LastIndex property to the next position of the last character of the matched text after the match. This means that you can iterate through all the matching text in a string by calling the Exec () method repeatedly. When exec () can no longer find a matching text, it returns null and resets the LastIndex property to 0. That is, even if it is a global schema, only one search is performed at a time.

Important: If you want to start retrieving a new string after you have completed a pattern match in a string, you must manually reset the LastIndex property to 0.

Tip: Note that exec () adds complete details to the array it returns, regardless of whether the regexpobject is a global mode. This is where EXEC () differs from String.match (), which returns much less information in global mode. So we can say that calling the Exec () method repeatedly in a loop is the only way to get complete pattern matching information for the global schema.

The test () method is used to detect whether a string matches a pattern. Returns True if the string contains text that matches regexpobject, otherwise false. Call the test () method of the RegExp object R and pass the string s to it, which is equivalent to this expression: (R.exec (s)! = null).

Methods for String objects that support regular expressions

Method Description return value return value contents (not found-1, position starting from 0)
Search Retrieves a value that matches a regular expression. Number The starting position of the string object that matches the string or regular expression that specifies the lookup.
Match Finds a match for one or more regular expressions. Array The array that holds the matching results.
Replace Replace the substring that matches the regular expression. String A new string that is replaced by the second argument (regexp/substr,replacement).
Split Splits a string into an array of strings. 1 4

The search () method does not perform a global match, and it ignores the flag G. It ignores the LastIndex property of RegExp and always retrieves from the beginning of the string, which means that it always returns the first matching position of the stringobject.

The match () method retrieves the specified value within a string, or finds a match for one or more regular expressions. The method is similar to IndexOf () and lastIndexOf (), but it returns the specified value instead of the position of the string. The return value is the array that holds the matching result. The contents of the array depend on whether the regexp has global flag G.

The match () method retrieves the string stringobject to find one or more text that matches the regexp. The behavior of this method depends to a large extent on whether RegExp has a flag g.

If RegExp does not have a flag G, then the match () method can only perform a match in Stringobject. If no matching text is found, match () returns NULL. Otherwise, it returns an array that holds information about the matching text it finds. The No. 0 element of the array holds the matching text, while the rest of the elements hold the text that matches the subexpression of the regular expression. In addition to these regular array elements, the returned array contains two object properties. The Index property declares the position of the starting character of the matching text in Stringobject, and the input property declares a reference to Stringobject.

If RegExp has the flag G, the match () method performs a global retrieval and finds all matching substrings in the stringobject. If no matching substring is found, NULL is returned. If one or more matching substrings are found, an array is returned. However, the contents of the array returned by the global match are very different from the former, and its array elements hold all the matching substrings in the stringobject, and there is no index attribute or input property.

Note: In global retrieval mode, match () does not provide information about the text that matches the subexpression, nor does it declare the location of each matched substring. If you need these globally retrieved information, you can use Regexp.exec ().

The replace () method is used to replace other characters with some characters in a string, or to replace a substring that matches a regular expression. A new string that is obtained after the first match or all matches of regexp have been replaced with replacement .

The replace () method of the string Stringobject performs a find-and-replace operation. It looks for substrings in Stringobject that match regexp, and then replaces them with replacement . If RegExp has global flag G, then the Replace () method replaces all matching substrings. Otherwise, it replaces only the first matched substring.

replacement can be a string, or it can be a function. If it is a string, then each match is replaced by a string. However, the $ character in replacement has a specific meaning. As shown in the following table, it shows that the resulting string from a pattern match will be used for substitution.

character Replace text
$, $ 、...、 The text that matches the 1th to 99th sub-expression in RegExp.
$& A substring that matches the regexp.
$` The text that is located to the left of the matching substring.
$ The text on the right side of the matching substring.
$$ Direct volume symbol.

Note: ECMAScript v3 stipulates that the parameter replacement of the replace () method can be a function instead of a string. In this case, each match calls the function, and the string it returns is used as the replacement text. The first parameter of the function is a string that matches the pattern. The next parameter is a string that matches the subexpression in the pattern and can have 0 or more of these parameters. The next argument is an integer that declares where the match appears in the Stringobject. The last parameter is the stringobject itself.

= ' aaa bbb CCC '; UWfunction (word) {    return word.substring (0,1). toUpperCase () +word.substring (1);}    ); document.write (UW); </script></body>

The example above is replaced with a function.

= "Doe, John";d ocument.write ( Name.replace (/(\w+) \s*, \s* (\w+)/, "$ $")); </script></body>

The previous example uses sub-expressions.

The split () method is used to split a string into an array of strings.

Stringobject.split (Separator,howmany). Separator: Required. A string or regular expression that splits stringobject from where specified by the parameter. Howmany: Optional. This parameter specifies the maximum length of the returned array. If this parameter is set, the returned substring will not be more than the array specified by this parameter. If this argument is not set, the entire string is split, regardless of its length.

The return value, an array of strings. The array is created by splitting the string stringobject into substrings at the boundary specified by separator . The string in the returned array does not include the separator itself. However, if separator is a regular expression that contains a subexpression, the returned array includes the strings that match those sub-expressions (but not the text that matches the entire regular expression).

Note: If you use an empty string ("") as a separator, each character in the Stringobject will be split.

Note: The action performed by String.Split () is the opposite of what Array.join does.

It also mentions the sub-expression concept of a regular expression, what is a subexpression?

It's easy to think about the things that must be matched and write a regular expression that matches the expected results, but it's much more difficult to think about situations that don't need to be matched and to make sure they're all out of the match. Sub-expression, defined with (), and used in Vim (\). Common use: To make precise setting and control on the object of repetition number metacharacters; the OR condition of the | operator is precisely defined and, if necessary, the subexpression allows nesting.

RegExp Regular Expression Object

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.