Regular expressions in JavaScript

Source: Internet
Author: User

How to use

1. Create an expression  

Methods for creating regular expression classes in javascript:

var regex = new RegExp ("\\d{5}") or 2.var regex =/\d{5}/(Recommended)

/Expression/is the syntax provided specifically for simplifying regular expression writing in JavaScript, and regular expressions written in//are not escaped by the Tube escape character.

Methods for RegExp objects:

2. determine if the match

Test (STR) determines whether the string str matches the regular expression, equivalent to IsMatch

var regex =/[email protected]+/;

Alert (Regex.test ("[email protected]");

Alert (Regex.test ("ab.com"));

3. Get matching results

EXEC (str) makes a search match, and the return value is the matching result (*), which corresponds to C # in match () and matches ()

If EXEC () finds the matched text, it returns an array of results (exactly matched strings and the results of the extraction group). )。 Otherwise, NULL is returned. To extract multiple calls, exec () is similar to the matches () method repeatedly.


Note the global mode/...../g

In non-global mode, calling exec () equals match ();

Multiple calls in global mode equal to matches ()


---i ignore case

---m multi-line matching

Regular expressions in JavaScript

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.