JS Basic--regexp

Source: Internet
Author: User

One, RegExp type

1. Literal method

var expression=/pattern/flags;

Where the pattern section can be any simple or complex regular expression, each regular expression can have one or more flags (flags)

G: Represents the global mode, which will be applied with all strings.

I: Indicates case insensitive (case-insensitive) mode, ignoring case when matching string.

M: Represents multiline (multiline) mode, and when a line outs is reached, it continues to find out if there are any items in the next row that match the pattern.

/* * Matches all instances of "at" in the string */ var pattern1=/at/G; /* * matches the first "bat" or "cat" is case insensitive */ var pattern2=/[bc]at/i; /* * matches all combinations of 3 characters ending with "at", not case-sensitive */ .  var pattern3=/.at/gi;

All meta characters used in the pattern must be escaped. The metacharacters in a regular expression include:

([{\^$|)? *+.]}

If you want to match these characters, you need to escape them, escaping with "\" for

* match the first "bat" or "cat" case-insensitive */var pattern1=/[bc]at/i; /* * Match First "[Bc]at", case insensitive */ var pattern2=/\[bc\]at/i; /* * matches all combinations of 3 characters ending with "at", not case-sensitive */ .  var pattern3=/.at/gi; /* * Match all ". At", case insensitive */ .  var pattern4=/\.at/gi;

2. The method of constructing function

Where the escape of special characters is denoted by "\ \"

/* * matches the first "bat" or "cat" is case insensitive */ var pattern1=/[bc]at/i; /* * Same as PATTERN1, just created with constructor function */ var pattern2=New RegExp ("[Bc]at", "I");

In constructor method, for metacharacters, escape with//

Using regular expression literals is not the same as using regular expressions created with the RegExp constructor. Regular expression literals always share a regexp instance, and every new RegExp instance created with the constructor is a new instance

Second, regexp instance properties

Each instance of RegExp has the following properties

    • Global: Boolean value that indicates whether the G flag is set.
    • IgnoreCase: Boolean value that indicates whether the I flag is set.
    • LastIndex: An integer that represents the character position at which to start searching for the next occurrence, starting from 0.
    • Multiline: Boolean value that indicates whether the M flag is set.
    • Source: A string representation of a regular expression that is returned in literal form rather than in the string pattern in the incoming constructor.

Third, regexp example method

1, EXEC (), the method is specifically designed for capturing groups, exec () receives a parameter, that is, the string to apply the pattern, and then returns an array containing the first matching information, or returns null if no match is found

var text= "Mom and Dad and Baby"; var pattern=/mom (and Dad (and baby)?)? /gi;

2, ToString (), and toLocaleString () return the regular expression literal, regardless of how the regular expression is created

Iv. RegExp Constructor Properties

V. Limitations of the Model

Although JS's regular expression function is quite complete, but still lacks the characteristic of the high-level regular expression, here does not introduce each.

JS Basic--regexp

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.