JavaScript Regular expression Knowledge Point collation

Source: Internet
Author: User

1 Regular Expressions

The use of a single string to describe, match a series of symbols of a syntactic rule of a string, can be easily understood as a rule to match the conditions of the string.

ps:https://regexper.com/to help understand regular expressions, regular expression tools

2 RegExp objects

JavaScript supports regular expressions through built-in object regexp, and there are two ways to instantiate RegExp objects: literals, constructors

3 Pre-defined classes

. equivalent to [^\r\n] all characters except carriage return and line break

\d equivalent to [0-9] is a numeric character

\d equivalent to [^0-9] is not a numeric character

\s equivalent to [\t\n\x0b\f\r] is the white space character

\s equivalent to [^\t\n\x0b\f\r] is not a whitespace character

\w equivalent to [a-za-z_0-9] is a word character

\w equivalent to [^a-za-z_0-9] is not a word character

4 Boundary characters

^ that starts with xxx

$ that ends with XXX

\b is the word boundary

\b is not a word boundary

5 quantifier

? Occurs 0 or 1 times, up to 1 times

+ occurs 1 or more times, at least 1 times

* that appears 0 or more times, any time

{n} appears n times

{N,m} occurs n to M times

{n,} that appears at least n times

6 Greedy Mode

\d{3,6}//As many matches as possible

7 Non-greedy mode

Match the regular expression as little as possible, that is, once the match succeeds, the attempt is no longer continued. Add after the quantifier? Can.

' 123456789 '. Match (/\d{3,5}?/g)

8 Grouping

Use () to achieve the function of grouping, so that quantifiers Act on grouping

(Byron) {3}

9 or

Use | To achieve or effect

byron| Casper Byr (on| Ca) Sper

10 Reverse Reference

2015-12-25 = "12/25/2015

' 2015-12-25 '. Replace (/(\d{4})-(\d{2})-(\d{2})/g, ' $2$3$1 ')

11 Ignore grouping

Don't want to capture certain groupings, just add them within the group? : You can

(? : Byron). (OK)

12 Outlook

The regular expression is parsed from the end of the text to the end of the text, which is called the "front". The forward-looking is to check whether the assertion is consistent when the regular expression is matched to the rule.

Compliant and non-conforming specific assertions are called positive/positive matching and negative/negative matching.

Forward forward: exp (? =asser)

Negative forward: exp (!=asser)

Console.log (' a2*3 '. Replace (/\w (? =\d)/g, ' X ')); X2*3 console.log (' a2*34v8 '. Replace (/\w (? =\d)/g, ' X '));//x2*x4x8 console.log (' a2*34vv '. Replace (/\w (? =\d)/g, ' X ')) ;//x2*x4vv console.log (' a2*34vv '. Replace (/\w (?! \d)/g, ' X ');//ax*3xxx

13 Object Properties

Global: Whether to search globally, default false;

Ignore: Is case sensitive, default false;

Multiline: Multi-line search, default value false;

LastIndex: Is the next position of the last character of the current expression matching content

Source: The literal string of the regular expression;

14 Regular Object Methods

(1) RegExp.prototype.exec (str): Performs a search on a string using a regular expression and updates the writing of the global RegExp object to match the result. Returns null if there is no matching text, otherwise returns an array of results. The index declaration matches the position of the first character of the text, and input holds the retrieved string.

Non-global invocation: Returns an array when the Exec () of a non-global regexp object is called, the first element is the text that matches the regular expression, and the second element is the text that matches the first subexpression of Regexpobject, if any. The third element is the text (if any) that matches the second sub-expression of the RegExp object, and so on.

var reg3=/\d (\w) \d/; var reg4=/\d (\w) \d/g; var ts= ' 1a2b3c4d5e '; var ret=reg3.exec (TS);  Console.log (Reg3.lastindex + ' \ t ' + Ret.index + ' \ t ' + ret.tostring ());//0 0  1a2,a console.log (reg3.lastindex + ' \ t ' + Ret.index + ' \ t ' + ret.tostring ())//0 0  1a2,a while (ret = reg4.exec (ts)) {     Console.log (reg4.lastindex + ' \ t ' + RE T.index + ' \ t ' + ret.tostring ());//3 0  1a2,a   //7    4  3c4,c}

(2) RegExp.prototype.test (str): A string used to test for the existence of a matching regular expression pattern in a string parameter. Returns true if it exists, otherwise false.

String and regular correlation method

(1) String.prototype.search (REG): Used to retrieve a substring specified in a string, or to retrieve a substring that matches a regular expression; The method returns the first match result index, and the lookup is not returned -1;search () method does not perform a global match. It ignores the G flag and is always retrieved from the beginning of the string.

Console.log ("JavaScript". Search (/script/i));//4

(2) String.prototype.match (REG): The match () method retrieves a string to find one or more text that matches the regexp, and whether RegExp has a flag g has a significant effect on the result.

If the regexp does not have a G flag, the match () method can only perform one match in the string, or null if no matching text is found, otherwise an array is returned with information about the matching text it finds. The first element of the returned 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 the regular array elements, the returned array includes 2 object properties, and the index declares the starting character of the matched text at the position of the string, Input declares a reference to Stringobject.

If RegExp has global flag G, the match () method performs a global retrieval, finds all matching substrings in the string, returns null if no matching substring is found, and returns an array if one or more matching substrings are found. The array element holds all matching substrings in the string, and there is no index or input property.

var reg3=/\d (\w) \d/; var reg4=/\d (\w) \d/g; var ts= ' $1a2b3c4d5e '; var ret=ts.match (REG3); Console.log (ret);//[' 1a2 ', ' a ', index:1, input: ' $1a2b3c4d5e '] console.log (ret.index + ' \ t ' +reg3.lastindex);//1  0  ret = Ts.match (REG4); Console.log (ret);//[' 1a2 ', ' 3c4 '] console.log (ret.index + ' \ t ' + reg4.lastindex);//undefined 0

(3) String.prototype.split (REG): Often use the split () method to split a string into a character array,

Console.log (' A,b,c,d '. Split (', '));  ["A", "B", "C", "D"] console.log (' A1b2c3d '. Split (/\d/)); ["A", "B", "C", "D"]

(4) String.prototype.replace ():

String.prototype.replace (STR,REPLACESTR); String.prototype.replace (REG,REPLACESTR) String.prototype.replace (reg,function)

Where the function parameter means that function will be called every time the match is replaced, there are four parameters: matching string, regular expression grouping content, no grouping without the parameter, the match in the string index, the original string.

JavaScript Regular expression knowledge-point grooming

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.