JavaScript Regular Expression notes

Source: Internet
Author: User

1. Recording

In ECMAScript 3,
Regular expression literals always share the same regexp instance, and each new RegExp instance created with the constructor is a new instance, and if you want to start retrieving a new string after a pattern match is completed in a string, you must manually reset the LastIndex property to 0.
ECMAScript 5 explicitly stipulates that the use of regular expression literals must be the same as calling the RegExp constructor directly, each time creating an
Create a new RegExp instance. ie9+, Firefox 4+, and chrome have made changes accordingly.
For the Exec () method, it returns only one match at a time, even if the global flag (g) is set in the pattern. In not
When you set a global flag, calling exec () multiple times on the same string will always return information for the first occurrence. and in setting
The global flag, each call to EXEC () will continue to look for new matches in the string until the end of the string is searched. In the global
In match mode, the value of lastindex is incremented each time the exec () is called, while in non-global mode it remains unchanged.
The JavaScript implementation of IE has a bias on the lastindex attribute, even in non-global mode,
The Lastindex property also changes every time.

Match double-byte characters (including kanji): [^\x00-\xff]

Console.info ("ABC kanji". Replace (/[^\x00-\xff]/g, "AA"). Length)//7

Match Chinese characters: [\U4E00-\U9FA5]

2. Regular replacement of some questions about non-matching

For example, the IMG tag inside a string is found and a label on its coat

Start thinking about match matching to the IMG tag and then get the results recycled, later to see if you can directly replace the implementation

The non-matching of JavaScript does not match multiple characters as a whole, such as [^img] only matches a string that does not appear I and has no M and no G

Can not match img as a whole does not appear, and later only in the following way to achieve the first judgment < symbols, no < symbol of any string 0 or more, and then judge the occurrence of < matches but not the IMG character string 0 or more, here used to 0 wide assertion, recorded:

' Daimgsdafd?s<a>asd</a>sfdsbasdfimggfg<fd> Dddasdfasdf '. Replace (/([^<]* (<?! IMG) *) * (]*\/>) ([^<]*] (< (?! IMG) *) */g, ' <a href= ' # ' >$3</a> ');

Do not know if there is any better or other way to implement with a replace.

3. Review of Knowledge points

1.0 Wide Assertion

(? =test) match is followed by Test but not captured

(?! Test) match is not captured after test

(excerpt from regular 30 minutes): used to find something before or after something (but not including the content), that is, they are used to specify a location like \b,^,$.

For example, the thousand-character: ' 122231223123 '. Replace (/(? = (?! ^) (\d{3}) +$)/g, ', ');

2. Greed and laziness

*, + for greedy to represent as many matches as possible

*? + Show as few matches as possible for laziness

Such as:

"<div>a</div><div>b</div>". Match (/<div>.*<\/div>/); Get ["<div>a</div><div>b</div>"]

. * indicates that 0 or more characters except carriage return will match the character as many as possible until the end of the </div> is met so that it matches the last </div>

"<div>a</div><div>b</div>". Match (/<div>.*?<\/div>/); Get ["<div>a</div>"]

.*? Indicates that 0 or more characters in addition to the carriage return match the characters as few as possible until the </div> end is met so that the first occurrence </div>

3. match and exec,replace

When match does not have a global match, the effect and exec return the matching array, the first element is the matching expression that first appears, and the second element is the subexpression of the first matched expression, if any, the third element, and so on.

such as: "ABC abc". Match (/A (BC)/); return ["abc", "BC"] ; ABC ABC does not match the second ABC

When match has a global match, a matching array is returned, and each element of the array corresponds to a matching expression, at which time the subexpression will not capture

Such as:"ABC abc". Match (/A (BC)/g) return  [ "abc ", " abc "

Replace and match have a different global match, and replace will catch sub-expressions regardless of global or non-global matches

such as: "abc abc". Replace (/A (BC)/, "$"); Get "BC ABC"

"ABC abc". Replace (/A (BC)/g, "$"); Get "BC BC"

"Aaabac". Replace (/(\w) \1+/g, '-') get "-bac"

In addition, the regular object will save the index of the last match after matching, so it is best to set the lastindex value of the regular object to 0 when using the same regular object again.

Eg:var a = new RegExp ("B", "G");

Console.log (A.test (' abc ')); True

Console.log (A.lastindex); 2

Console.log (A.test (' abc ')); False

Console.log (A.lastindex)//0

4. Summary of Knowledge points

1.exec

Grammar:

Reg.exec (str)

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.

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 ().

When Regexpobject is a global regular expression. 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.

eg

/123/.exec (' 123 123 '); return ["123"]

Global Reg If you want to match all the implementations to be matched by a loop:

var a;var r=[];      var str= "123 123 ab1232"; var reg = new RegExp ("123", "G"); while ((A=reg.exec (str))!=null) {Console.log (a);  Console.log (Reg.lastindex); R.push (A[0]);} Console.log (R)//all matching

2.match

Grammar:

Stringobject.match (RegExp)

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.

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.

3.0 Wide Assertion

The so-called 0 wide assertion is that the width of the capture is 0, that is, only the match is not captured, and the parentheses do not cause grouping.

(? =x) forward assertion, assuming that the position is followed by an X

(?! x) forward negative assertion, assuming that the position is followed by an X

(? <=x) Reverse assertion, assuming that the position is followed by an X

(? <! x) Reverse negation assertion, assuming that the position is not followed by X

Unfortunately JavaScript does not support reverse assertion (? <=x) and (? <! X

There is another (?: X) in JS that should not be called a 0 wide assertion because it will catch.

(?: x) is the location where x is present, and X is captured, but the parentheses here do not cause grouping.

Like what:

/123 (a)/.exec (' 123 123a '); return ["123a", "a"]//Match 123 followed by a string, regular parentheses cause grouping

/123 (?: a)/.exec (' 123 123a '); returns ["123a"] match 123 followed by a string and captures a but does not group

/123 (?!     A)/.exec (' 123a 123 '); returns ["123"] matches a string that is not a after that position, that is, a string that is not a after the regular ' 123 '. Not capturing a nor causing grouping

/123 (=a)/.exec (' 123a 123 '); return ["123"] matches the string after the position is a, that is, after the regular ' 123 ' is a string. Not capturing a nor causing grouping

Apps that match HTML:

/

Five. Additional Knowledge points:

Using RegExp's explicit constructor, the syntax is: New REGEXP ("pattern" [, "Flags"]).

Use RegExp's implicit constructor, in plain-text format:/pattern/[flags].

The pattern section is required for the regular expression pattern text to be used.

In the first way, the pattern part is in the form of a JavaScript string, which needs to be enclosed in double or single quotes, and no '/' is required.

In the second way, the pattern part is nested between two "/" and cannot use quotation marks.

Because "\" in the JavaScript string is an escape character, when you create an RegExp instance object with an explicit constructor, you should replace "\" with "\" in the original regular expression

such as: var reg1 = new RegExp ("\\d{2}");

var reg2 =/\d{2}/;

var reg = new RegExp ("^ (\\+|-)? \\d+ (\.\\d{0,2})? $"); Floating-point number with two decimal places

When a variable is included:

var a = "abc",

Reg3 = new RegExp ("^\\d" + A + "\\d$");

Because the escape character in the regular expression pattern text is also "\", if the literal character "\" is to be matched in the regular expression pattern text, "\ \" is to be represented in the Regex-mode literal, and "\\\\" is used to represent the literal character when the RegExp instance object is created using an explicit constructor " \”。

var reg = new RegExp (\\\\).

Here's a review of the most common test exec match search replace indexOf split 7 methods

var str = "", str1 = ""; String

var reg = new RegExp (); Reg Object

var x = []; The returned array

G//Global match

I//Case insensitive

M//Multiline pattern matching

1.test

Format: reg.test (str); Returns TRUE or false;

Eg:/(a) \w+/.test ("ABCDE")//return true;

2.exec

Format: reg.exec (str); Returns an array array the first item is a matched expression if there is a matching subexpression, the second item of the array is a subexpression and so on. exec always returns information related to the first match, whether or not G

Eg:/a (b) \w+/.exec ("abcdef"); The returned array x[0] = "abcdef", x[1] = "B";

/ab\w+/g.exec ("ABCDE.FABC")//Return to "ABCDE"

3.match

Format: Str.match (REG); Returns an array if no global identifier G is the same as exec, the first item is a matching expression, and if there is a matching subexpression, the second item of the array is a subexpression and so on. If Reg has a global identifier G then the first is the first matching expression, the second is the second matching expression, and so on.

Eg: "Abcdef.abc". Match (/A (b) \w+/); return x[0] = "abcdef", x[1] = "B";

"Abcdef.abc". Match (/A (b) \w+/g); return x[0] = "abcdef", x[1] = "ABC";

About the difference between exec and match here's an article that says more detail: http://makemyownlife.iteye.com/blog/395172

4.search

Format: Str.search (REG); Returns the position of the specified value from the string, or 1 if not found. It ignores the flag G. It also ignores the lastindex property of RegExp and always retrieves it from the beginning of the string.

Eg: "abcdef". Search (/a/)//Return 0

5. IndexOf

Format: Str.indexof (Str1[,startindex]); Returns the position of the specified value from the string, or 1 if not found, specifying the location to start the search, and the difference between search is that str1 cannot be a regular expression.

6.replace

Format: Str.replace (REG,STR1); Returns the replaced string support I, G, M

Eg: "abcdef". Replace (/a/, "B") returns: "Bbcdef";

"Abcdefaa". Replace (/a/ig, "B") returns: "BBCDEFBB";

When STR1 is a function, the text that matches the regular expression is returned, and the text is matched by the subexpression, and so on.

When STR1 is $1$2 ..., the $1$2 matches the corresponding sub-expression respectively.

eg

var a = "Abcealsk";

var C1 = a.replace (/(AB) c/ig,function ($1,$2) {return $ + $}); return: Abcabealsk

var C2 = a.replace (/(AB) C/ig, "$1$2"); return: Ab$2ealsk//At this time, no corresponding word expression is returned as a string.

For example, commonly used to remove both ends of the string space

var a = "a A";
var B = a.replace (/(^\s*) | (  \s*$)/g, "); or var B = a.replace (/^\s* (\w+.*\w+) *\s*$/, ' $ ');

B is ' a A '

7.split

Format: Str.split (REG); Returns the array supported by Reg

Eg: "A_b_b_d_e". Split (/b/i); return: [A_,_,_d_e]

Practice:

Test for duplicate characters:

var a = "ABCDEFGA";

var c = "ABCDEFG";

var B =/(\w) \w*\1/;

Console.log (B.test (a))//true

Console.log (B.test (c))//false

\m represents the content of the first m match

eg

/(span) \1/.test (' Spanspan ')//true

/(span) (DIV) (TD) \1\2\3/.test (' spandivtdspandivtd '); True

eg matching xhtml tag/^< (\w+) \s*.*>.*<\/\1>|<\w+\s*\/>$/.test ("<span></span>")

JavaScript Regular Expression notes

Related Article

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.