Guidelines for using regular expressions in JavaScript _javascript tips

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 in JavaScript specifically to simplify regular expression writing, and regular expressions written in//are not used for escape characters.

Ways to RegExp objects:

2. Judge whether the match

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

Copy Code code as follows:

3. Get match result

Exec (str) for search match, return value is match result (*), equivalent to match () and matches ()

In C # if EXEC () If a matching text is found, it returns an array of results (the exact matching string and the result of the extraction group). )。 Otherwise, NULL is returned. To extract multiple calls to Exec () is similar to the matches () method.

Note Global mode        /...../g

In non-global mode, calling exec () is equivalent to match ();

Repeated calls in global mode equivalent to matches ()

---I ignore case

---m multiline match

The above is the introduction of the use of the JavaScript's right expression, I hope you can enjoy it.

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.