The regular of JS

Source: Internet
Author: User

Concept: Regular Expressions: Use some special symbols (characters) to represent specific content to find and match a string that matches a rule.

The definition of a regular expression:

1 Direct definition / Regular expression is content /[modifier] Recommended use Eg:var reg=/\d/g

2 object definition var reg=new RegExp (string, modifier)

Two escape characters

\d Digital

\d Non-digital

\s Spaces

\s Non-whitespace

\w numbers, letters, underscores

\w non-numeric, non-alphabetic, non-underlined

\b The boundaries of words, independent parts (starting position, end position, space)

\b Part of the non-boundary

    . Any one character

\. The real point

Three classifiers

+ {1,} Repeat at least 1 times, up to No limit

? {0,1} repeats at least 0 times and repeats up to 1 times

* {0,} Repeat at least 0 times, up to No limit

{n,} repeats at least n times, up to No limit

{n,m} repeats at least n times and repeats up to M times

| or eg:a|b.

^ Match start

$ match End

[] matches any one of the characters in the brackets, representing any one character [A-z] between one character [1-9] 1-9 and any letter [a-z] A-Z any letter

[^1-2] Take the anti-exclusion number

Four matching Chinese

[\U4E00-\U9FA5] any of the Chinese characters

Five methods

1 Test

To see if the regular matches the specified string

Syntax: regular. Test ("string")

Return value: True success

False failed

1 var str="xubj"; 2 // true 3 // false

2 Search Find the location of the first occurrence of a matching string

String. Search (string or/Regular/)

return value:

Find the return subscript

Return not found-1

var str="nihaoxubj"; Console.log (Str.search ("i ")); // 1 return subscript console.log (Str.search (/\d/g)); // -1 not found return-1

3 match matches a string or regular, placing the matched result in an array

Syntax string. Match (string or regular)

return value:

Finds the returned array of results found

Null not Found

var str="xubjsdsfdsxubjsd25"; Console.log (Str.match (// ["Xubj", index:0, Input: "XUBJSDSFDSXUBJSD25"] // ["Xubj", "XUBJ"] // NULL

4 Replace replaces the match to the string

Syntax string. replace (string or regular, string or function)

Return value: The new string after substitution, the original string is unchanged

Note: The second argument is a function, it must have a return value, otherwise it will replace the original content with undefined

varStr="XUBJ"; Console.log (Str.replace (/x/,0));//0UBJ Returns the changed stringConsole.log (str);//xubj Original string unchangedvarNewstr=str.replace (/x/, function () {return 'F';}); Console.log (NEWSTR);//FUBJvarNewstr1=str.replace (/x/,function ($0,$1,$2) {Console.log ($0,$1,$2);//x 0 xubj    /*parameter 1 to match content, same as the first parameter of replace parameter 2 matches the location of the corresponding position subscript parameter 3 the original string*/});//example to turn x in a string into a *varStr="XUBJNIHAOXUBJ";varNewstr2=str.replace (/xu/g,function ($0,$1,$2){     varStr="";  for(varI=0; i<$0. length;i++) {str+="*"; }     returnstr;}); Console.log (NEWSTR2); //**BJNIHAO**BJ

Two groups and children

Group with ()

The child is placed in parentheses, and each parenthesis can be considered a child

Replace method matches Subkeys

Parameter starts with the second one, and corresponds to each child

//Replace method matches subkeysvarreg=/(\d+) (-)/G;varStr="2017-12-08";varNewstr=str.replace (Reg,function ($0,$1,$2){    //Console.log ($);//2017-12-Same as the contents of the regular (to match the content)//Console.log ($);//2017 12 content in the first group//Console.log ($);//--content in the second group    return$1+'/';}); Console.log (NEWSTR); //2017/12/08

Match method matches subkey

There are no children in the array returned by G

There are no children in the array returned by G

// Match method matches subkey var str="xubj"; Console.log (Str.match (//["Xubj", "Xu", "B", "J"] no G returns the subkey Console.log (Str.match (/(Xu) (b) (j)/g)); // ["XUBJ"]  has G does not return children
//formatted Datevard1="2017-12-08";varD2="2017+++12++++08";vard3="2017////12//08";vard4="2017+---12+++-08";//Explain a bunch of numbers a bunch of non-numbers a bunch of numbers a bunch of non-numbers a bunch of numbersvarreg=/(\d+) \d+ (\d+) \d+ (\d+)/;varNewstr=d1.replace (Reg,function ($0,$1,$2,$3){    //$1,$2,$3 represents children in a group    return$1+'years'+$2+'Month'+$3+'Day';});

The regular of JS

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.