Regular expressions in JS and pattern considerations _ Regular Expressions

Source: Internet
Author: User

RegExp Object creation:

A regular regular expression can be created with a direct amount, a character surrounded by a slash "/". But in an environment where parameter changes are required, the REGEXP () constructor is a better choice:

var reg1 =/' \w+ '/g;
var reg2 = new RegExp (' \\w+\ ', ' G ');

Compare the two ways of creating, the first parameter in the RegExp is the regular string to be created, noting that, because it is not a representation of the direct amount, it is not surrounded by a slash "/", but rather that the quote "'" and escape symbol "\" must be escaped two times in the string.

In addition, both the direct volume and the RegExp () constructor generate a new RegExp object and assign it to a variable.

In the JavaScript Authority Guide, ECMAscript 3 says that for the direct amount of regular expressions, the same RegExp object is returned every time it is used, so a regular expression created with a direct amount shares an instance. It is not until ECMAScript 5 that each return of a different instance is specified.

In each browser, IE has been complying with ECMAScript 5, while older versions of other browsers follow the ECMAScript 3 rule. Therefore, in practice, it is safer to take the method of constructor creation, or to remember to 0 when using the lastindex attribute.

Use of parentheses ():

1. Group

That is, to combine individual items into a single subexpression, commonly used for? , +, *, {n,m}, etc. repeated processing. See Example:

var reg =/java (script)?/;

In the equation, the script is treated uniformly.

2, backward reference

That is, the text that matches the preceding brackets in the reference style in the regular expression with "\ n" (the ordinal number of the reference). See Example:

var reg =/(\d+) [a-z]{3}\1/; 
20man20
//20man23 wrong
//reg =/\1[a-z]{3} (\d+)/; wrong

Note "\ n" refers to the previously matched text "20" instead of the matching regular expression "\d+". In addition, JS can only refer to the previous matching text, such as the example of the \1 written in parentheses in front of the reference, will not match any text, the browser prompts an error. Similarly, JS does not support reference naming rules like "(? <name>exp)" (exp is a regular character), only references to numbers are supported.

Now that you've mentioned grouping and references, if you want to group only, and you don't want to, you can use the form of "(?: Exp)" to match neither the text nor the reference number. See Example:

var reg =/(\w{3}) (?: \ d+) ([a-z]{2}) \2/;
Man7788abab

Obviously \2 matches "AB" instead of "7788". This facilitates group processing and also speeds up query efficiency.

3, child mode matching

Sometimes we would like to refer directly to the text of the operation bracket matching, then we can use the Sub pattern matching function (in the authoritative guide is called the Child pattern match, a bit awkward, actually is to replace the matching text with a variable form). The basic form is in the form of ' $n ' to replace the text that matches the number n, and the Replace () method used in the string object, see example, exchanging words on both sides of the equal sign:

var reg =/(\w+) = (\w+)/;
var str = ' love=hate ';
Str.replace (Reg, ' $2=$1 ');
"Hate=love"

Order, greed, laziness:

Generic duplicate matching character Furu? , +, *, {n,m} in the matching process, the greedy matching method, that is, as many matches as possible to the result character. Corresponding to the lazy match, that is, the minimum number of matching results, the use of the form only to repeat the matching character after the question mark "?" "Can be, like??" + 、*? , {n,m}?. See Example:

var str = ' Goooogle ';
var reg1 =/o+/;   "Goooo"
var reg2 =/o+?/;  "Go"

Now let's change the example slightly:

var str = ' Goooogle ';
var reg1 =/o+gle/;  "Oooogle"
var reg2 =/o+?gle/;  "Oooogle"

After the change of the example result becomes the same, why/o+?gle/did not match to "ogle"? The original regular expression always matches from left to right, and does not get the substring from the right side to match.

Although the results above are the same, the matching principle is not quite the same. In Reg1, first o+ matches all "O" and then matches "gle" to complete the overall match. In REG2, o+ will match an "O" First, and then gle match the 2nd to 4th digits of the string (that is, the "Ooo" of the original string) failed. And then back to o+? To match the second "O", after the success of the 3rd to 4th match "Gle", and so on ... The last match to the entire string.

In general, keep in mind that, from the priority point, the order from left to right matches > greedy/lazy matches.

0 Wide Assertion:

The general explanation of the zero-width assertion is available in the blog "Regular expression 30 minutes introductory tutorial", and it's worth noting that JS only supports 0-wide lookahead assertions. 0 wide Positive lookahead assertion "(? =exp)" and 0 wide negative lookahead assertion "(?!) EXP) ".
The so-called "0 wide" is that it does not occupy space in the matching result characters. For example, "\w", "\s" will occupy one or several spaces, depending on the length of the matched characters. And like "^", "$" this corresponds to the first end of the position, does not occupy space, 0 width is this category.

The so-called "positive/negative prediction" refers to the situation in which the claim is satisfied. "Positive" means to satisfy exp, "negative" means not satisfying exp.

The so-called "first" refers to the string being matched at the front, 0 wide assertion followed by. That is, whether the latter part of the string satisfies the assertion.

The so-called "assertion" is the condition of judgment.

Look at two examples of 0-wide assertions:

var str = ' Java coffeescript ';
var reg1 =/\b\w+ (? =script\b)/; Coffee 
var reg2 =/\b\w+ (?! script\b)/; Java

REG1 is a zero-width positive lookahead assertion, "(? =script\b)" means that a word needs to end with "script," which represents a condition that does not occupy any amount of space.

Similarly, REG2 is a zero-width negative lookahead assertion, "(?!) script\b) "represents a word that does not end with" script. "

In addition, because there is no 0 wide speaks assertion, it is not possible to determine what conditions the preceding part of a string satisfies. But in JS can be implemented with a number of regular expressions: first match to the string to find, and then intercept the beginning to the index of the word subcode string, and then match the end of the substring to meet the required assertion conditions. Specific use can be another try.

Similarities and differences between match () and exec ():

Match and Exec are common methods for regular expression-matching strings. The two implementations are similar in functionality, with some subtle differences:

1, the use of the way

Match is the method of the string wrapper object, Usage: String.match (REGEXP);
Exec is the method of a regular expression object, using: Regexp.exec (String);

2. Results returned

When RegExp does not set the global flag "G":

The return result of the two is the same. Returns NULL when there is no matching value, and returns an array when there is a match. Array[0] for matching strings, array[1], array[2] ... The substring that matches the parentheses in the regular expression is $ $ ... The array has two properties at the same time, Array.index represents the initial position of the matching string, and Array.input represents the string being retrieved.

When RegExp has the global flag "G" set:

Match returns an array of arrays when a value is available. Each item in the array, in turn, represents all of the strings that are matched, so there is no longer a string of parentheses to match. The array does not have the Index property and the input property at this time.

exec is no different than the global label "G" performance. At this point, the array array,array[0] is the current matching string, array[1],array[2] ... A string that matches the current matching parentheses. Notice the Lastindex property of the RegExp object, representing the last position at the end of the matched string in the original string. When there is no further matching result, the Lastindex property is set to 0. Therefore, you can use the lastindex loop to find all the matching strings. Take a look at the example:

var str = ' I love1 my job22 ';
var reg =/\b[a-z]+ (\d+) \b/g;
Array = Str.match (reg);
Array = ["Love1", "Job22"] 
//array.index = undefind
//array.input = undefined
------------------------- -----------
array = reg.exec (str);
Array = ["Love1", "1"]
//array.index = 2
//array.input = "I love1 my job22"
//reg.lastindex = 7
//run Again
reg.exec (str);
Array = ["Job22", "//array.index"]
=
//array.input = "I love1 my job22"
//reg.lastindex =
/run again
reg.exec (str);
Reg.lastindex = 0

Finally, taking into account the ECMAScript 3 and ECMAScript 5 version of the difference, after each match to remember to manually regexp object lastindex attribute to 0, to meet the requirements of the old non-IE browser.

JS regular expressions, pattern, precautions

In a word, use,/\w+@+\w+ (\.+\w+) {1,}/.test (str) to verify, do not use "\w+@+\w+ (\.+\w+) {1,}". Test (str) to verify;
Use the latter, direct, with ADD@DFDDF can pass the verification;

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.