Guidelines for using regular expressions in JavaScript

Source: Internet
Author: User
Tags regular expression

This article mainly introduces the use of regular expressions in JavaScript, very detailed, with a simple example, recommended here.

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

The code is as follows:
var regex = /.+@.+/;
Alert (Regex.test ("a@b.com"));
Alert (Regex.test ("ab.com"));

3. Get matching Results

EXEC (str) searches for a match with the return value of the matching result (*), equivalent to match () and matches () in C #

If EXEC () finds a matching text, 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 are equivalent to matches ()

---i ignore case

---m multiple-line match

The above content is this article about the use of JavaScript in the expression of the introduction, I hope you can enjoy.

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.