Js regular functions (internal)

Source: Internet
Author: User
1) String method a.) String. search () parameter: it can be a regular expression or a normal String. Returned value: If a match is found, the index location of the first character is returned. If no match is found, the return value is-1 vars & quot; Helloworld. & quot; reg/world/I; // I indicates no size difference... syntax 1 .) string Method
A.) String. search ()
Parameter: it can be a regular expression or a normal string.
Returned value: If a match is found, the index location of the first character is returned. If no match is found,-1 is returned.
Var s = "Hello world .";
Reg =/world/I; // I indicates case insensitive
Alert (s. search (reg ));
B.) String. replace ()
Parameter: 1. Regular Expression 2. string used for replacement
Returns the replaced string.
Var s = "Hello world. World! ";
Reg =/world/gi; // g is in global mode and all matches are replaced.
Alert (s. replace (reg, 'A '));
Subexpression (the matching subexpression in brackets can be accessed through $ n ):
Var s = "11 22 33 44 55 66 77! ";
Reg =/(\ S +) (\ s +) (\ S +)/g; // I indicates case insensitive
Alert (s. replace (reg, '$3 $2 $ 1'); // 22 11 44 33 66 44 77!
C.) String. match ()
Put the matched string in the array and return it. If no match exists, null is returned.
// In global mode, each match is placed in the returned array.
Var s = "My phone number is 0631-1234567. Your phone number is 021-87654321. We often contact you! ";
Reg =/(0 \ d-\ d {8} | 0 \ d-\ d {7})/g; // note that this regular expression may be incomplete and may be easier to write.
Alert (s. match (reg); // 0631-1234567,021-87654321
// If no g tag is set, the returned array [0] is the overall match, and 1-N indicates the child match.
Var s = "My salary is $800, and her salary is $400! ";
Reg =/(\ $) (\ d +)/; // $ escape is required
Alert (s. match (reg); // $800, $, 800
D.) String. split ([delimiter [, limit on the number of returned elements])
// Common Segmentation
Var s = "My salary is $800, and her salary is $400! ";
Alert (s. split (',') [0]); // my salary is $800
// Use regular expressions
Var s = "China's population reaches 1.4 billion, but its per capita income is less than 3000 US dollars. Therefore, China is still in developing countries! ";
Alert (s. split (/\ d/); // China has a population of hundreds of millions, but its per capita income is less than USD, So China is still in a developing country!
2.) RegExp object
A.) RegExp. test (str)
Why? If tr matches this regular object, true is returned.
Var s = "This statement contains the number 123 for testing! ";
Var re =/\ d + /;
If (re. test (s ))
Alert ("contains numbers ");
B .w.regexp.exe c () is more powerful than String. match ().
// Non-Global exec, [0] returns a match, 1-N returns a child match
Var s = "number 123, letter abc, underline _! ";
Var re =/(\ w) \ w */;
Alert(re.exe c (s); // 123,1
Alert(re.exe c (s). input); // number 123, letter abc, underline _!
Alert(re.exe c (s). index); // 2
// With global tag
Var s = "number 123, letter abc, underline _! ";
Var re =/(\ w) \ w */g;
Alert (re. lastIndex); // 0
Alert(re.exe c (s); // 123,1
Alert (re. lastIndex); // 5
Alert(re.exe c (s); // abc,
Alert (re. lastIndex); // 11
Alert(re.exe c (s ));//_,_
Alert (re. lastIndex); // 16
C.) Regular Expression Constructor
Re = new RegExp ("pattern", ["flags"])
Var s = "number 123, letter abc, underline _! ";
Var re = new RegExp ("(\ w) \ w *", "g"); // because \ is also an escape character in quotation marks, you need to add one more \
Alert(re.exe c (s); // 123,1

From jiutian galaxy
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.