The regular in JS

Source: Internet
Author: User

| : or meaning, means that you want to match the keywords for multiple, and are all you want such as: Var str= ' 17 Chinese ships near Mahkota Reef are under the supervision of the Philippine Army ' var re =/Philippines | China ship | under surveillance/g; Str.replace (Re,function (str) {} * * * * 17 * * * * * * * * * * * * * * * * * * * * * * * * * *//Match subkey: Parenthesis () (another meaning, grouping operation)
1+1*2//(+) *//The whole of the regular is called (mother)//And then the first parenthesis inside the left is called the first child (mother's first child)//The second parenthesis is the second child var str = ' 2013-6-7 ';
var re =/(\d+) (-)/g;
str = str.replace (re,function ($0,$1,$2) {///First parameter: $ A (mother), second parameter: $ (first child)//, second parameter: $ (second child) alert ($);    Alert ($);        Alert ($);        return + '. ';    Return $0.substring (0,$0.length-1) + '. '; }); The first alter pops up "2013-" the second pop-up "6-" where the first of the $2013 is $6 for the second time-this is the meaning of the match subkey var str = ' abc ';
var re =/(a) (b) (c)/;
Alert (Str.match (re)); [Abc,a,b,c] (can get a collection of children when match does not have G)

var kwords=str.match (REG);
1. reg does not add G,match also only find the first one
When to use: when you want to get the content and location of the first keyword
Return value: An array that contains the contents of the Found keyword
kwords:["keywords"]
kwords.index-> keyword location if not found, return to null!
2. Reg Plus G,match will find the contents of all the keywords
When to use: when you want to get the content of all your keywords
Return value: An array that contains the contents of all the keywords
The length property of the array, which records the number of all keywords
Cons: Only the content of all keywords can be found, the location of each keyword cannot be found.
Emphasis: As long as you use match, you can return NULL, you must first determine that it is not NULL, and then perform subsequent operations!

//. : Any character//\. : The real point
/*var str = ' A.C ';
var re =/a\.c/;
Alert (Re.test (str)); */
\b: Independent part (start, end, space)//\b: Non-independent part
var str = ' Onetwo ';
var re =/one\b/;
Alert (Re.test (str)); var re =/sclass/;  When you need to pass the argument, be sure to use the full name of the var re = new RegExp (' \\b ' +sclass+ ' \\b '); Note that the full name needs to be escaped.//\1: Duplicate First subkey//\2: Duplicate Second subkey
/*var str = ' ABCA ';
var re =/(a) (b) (c) \2/;
Alert (Re.test (str)); */
/*var re =/\w\w/; C9var re =/(\w) \1/; CC 99

The regular in 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.