JavaScript RegExp Regular Expression Object Detail _ regular expression

Source: Internet
Author: User
Tags modifiers
Direct volume syntax

/pattern/attributes

To create the syntax for a RegExp object:
Copy Code code as follows:

var obj=new RegExp (pattern,attributes);

Case 1:
Copy Code code as follows:

var pattern=/a/;
document.write (Pattern.exec ("Asdas"));

Case 2:
Copy Code code as follows:

var obj=new RegExp ("a");
document.write (Obj.exec ("Asdas"));


The execution results of Case 1 and Case 2 are the same;
Parameters
Parameter pattern is a string that specifies the schema or other regular expression of the regular expression.

The parameter attributes is an optional string that contains the property "G" (performs a global match lookup for all matches rather than stopping after the first match is found.) , "I": Performs a case-insensitive match. and "M": Perform multiple line matches. , used to specify global matches, case-sensitive matches, and multiple line matches, respectively. The M attribute is not supported until ECMAScript is standardized. If pattern is a regular expression, rather than a string, you must omit the argument.

return value
A new RegExp object with the specified pattern and flags. If 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.

If you do not use the new operator and REGEXP () as a function call, its behavior is the same as when called with the new operator, except that when pattern is a regular expression, it returns only the formula and no longer creates a new RegExp object.

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

TypeError-This exception is thrown if pattern is a RegExp object, but the attributes argument is not omitted.
RegExp Object Method:
1. Test (): Retrieves the value specified in the string. Returns TRUE or FALSE.
Copy Code code as follows:

var patt1=new RegExp ("E");
document.write (Patt1.test ("The best things in life are Free");

2, EXEC (): Retrieves the value specified in the string. Returns the found value and determines its location.
Copy Code code as follows:
Method retrieves the specified value in a string. The return value is the value that was found. If no match is found, NULL is returned.

3, compile (): You can change the retrieval mode, you can add or delete the second parameter.
Copy Code code as follows:

var patt1=new RegExp ("E");
document.write (Patt1.test ("The best things in life are Free");
Patt1.compile ("D");
document.write (Patt1.test ("The best things in life are Free");

Modifiers:

1. G: Perform a global match to find all matches rather than stopping after the first match is found;
When you use the "G" parameter, the EXEC () works as follows:
Find the first "E" and store its location
If you run exec again (), the search starts at the stored location, finds the next "E", and stores its location
Copy Code code as follows:

var patt1=new RegExp ("E", "G");
Todo
{
Result=patt1.exec ("The best things in life are Free");
document.write (result);
}
while (Result!=null)

RegExp Object Properties
Whether the 1:global:regexp object has a flag g.
Whether the 2:ignorecase:regexp object has a flag I.
3:lastindex: An integer that marks the starting position of the next matching character.
Whether the 4:multiline:regexp object has a flag m;
5:source: The source text of the regular expression.

Copy Code code as follows:

var obj=new RegExp (/s/);
document.write (Obj.source);

methods that support String objects of regular expressions
The 1:search () method retrieves the substring specified in the string, or retrieves a substring that matches the regular expression.
Ignore Case case:
Copy Code code as follows:

<script type= "Text/javascript" >
var str= "Visit w3school!"
document.write (Str.search (/w3school/i))
</script>

return value

The starting position of the first substring in the stringobject that matches the regexp.

Note: If no matching substring is found, return-1.

Description

The search () method does not perform a global match, and it ignores flag g. It ignores the RegExp lastindex property at the same time and always retrieves it from the beginning of the string, which means it always returns the first matching position of Stringobject.

The 2: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, not the position of the string.

return value

An array that holds the matching results. The contents of the array depend on whether RegExp has global flag G.

Description

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 great extent on whether the regexp has a sign 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 remaining elements hold the text that matches the subexpression of the regular expression. In addition to these regular array elements, the returned array also contains two object properties. The Index property declares the position of the starting character of the matching text in the Stringobject, and the input property declares a reference to the Stringobject.

If the regexp has a flag G, the match () method performs a global search and finds all the 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 contain 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 position of each matching substring. If you need information for these global searches, you can use Regexp.exec ().

argument is a string case:

Copy Code code as follows:

var str= "Hello world!"
document.write (Str.match ("World") + "<br/>")

The argument is a regular expression case:
Copy Code code as follows:

var str= "1 plus 2 equal 3"
document.write (Str.match (/\d+/g))

The 3:replace () method replaces some characters in a string with some other characters, or replaces a substring that matches a regular expression.

Stringobject.replace (regexp/substr,replacement)
 
 
Parameters Description
Regexp/substr

Necessary. The RegExp object that prescribes the substring or pattern to be replaced.

Note that if the value is a string, it is used as the direct text pattern to retrieve, not first converted to the RegExp object.

Replacement Necessary. A string value. Provides a function that replaces text or generates alternate text.

return value

A new string that is replaced with replacement for the first match of RegExp or after all matches have been made.

Description

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

replacement can be either a string or 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 string that is matched from the pattern will be used for substitution.

character Replace text
$, $ 、...、 $99 Text that matches the 1th to 99th subexpression in RegExp.
$& The substring that matches the regexp.
$` The text located on the left side of the matching substring.
$' The text that is located to the right of the matching substring.
$$ The direct measure symbol.

Note: The ECMAScript v3 stipulates that the parameter replacement of the replace () method can be a function rather than 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 argument 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.

String Replacement:
Copy Code code as follows:

var str= "Visit microsoft!"
document.write (Str.replace (/microsoft/, "W3school"))

Global substitution:
Copy Code code as follows:

var str= "Welcome to microsoft! "
Str=str + "We are proud to announce" Microsoft has "
Str=str + "One of the" largest WEB developers sites in the world. "
document.write (Str.replace (/microsoft/g, "W3school"))

Returned results: Welcome to w3school! We are proud to announce this W3schoolhas one of the largest WEB developers in the world. Use regular Expressions:
Copy Code code as follows:

Name = ' "A", "B";
Name.replace ([^ "]*)"/g, "' $ '");

Use functions:
Copy Code code as follows:

Name = ' AAA bbb CCC ';
Uw=name.replace (/\b\w+\b/g, function (word) {
Return word.substring (0,1). toUpperCase () +word.substring (1);}
);

the 4:split () method is used to split a string into an array of strings.

Grammar

Stringobject.split (separator,howmany)

Parameters Description
Separator Necessary. A string or regular expression that splits the Stringobject from the place specified by the parameter.
Howmany Optional. This parameter specifies the maximum length of the array to return. If this argument 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.

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 a string that matches the subexpression (but does not include text that matches the entire regular expression).




Tips and comments


Note: If an empty string ("") is used as a separator, then each character in the Stringobject is split.


Note: The actions performed by String.Split () are the opposite of those performed by Array.join.



Modifiers


modifiers Description
I Performs a match that is insensitive to case.
G Performs a global match (finds all matches rather than stops after the first match is found).
M Performs multiple-line matching.



Brackets


Square brackets are used to find characters in a range:


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



Metacharacters


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


Meta character Description
. Finds a single character, except for line breaks and line terminator.
\w Find word characters.
\w Find non word characters.
\d Find numbers.
\d Looks up a non-numeric character.
\s Find white space characters.
\s Find Non-white-space characters.
\b Finds a match at the beginning or end of a word.
\b Finds a match that is not at the beginning or end of a word.
The Find NUL characters.
\ n Find line breaks.
\f Find page breaks.
\ r Find a return character.
\ t Find tabs.
\v Finds vertical tabs.
\xxx Find the characters specified in octal xxx.
\xdd Finds characters that are specified in the hexadecimal number DD.
\uxxxx Finds Unicode characters as specified in hexadecimal number xxxx.



Quantifiers


quantifiers 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} A string that matches a sequence that contains an X or Y N.
N{x,} A string that matches a sequence containing at least X N.
n$ Matches any string at the end of N.
^n Matches any string at the beginning of N.
? =n Matches any string immediately followed by the specified string n.
?! N Matches any string that is not followed by the specified string n.
JavaScript RegExp Object Reference Manual

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.