Basics of Regular Expressions in js, regular expressions in js

Source: Internet
Author: User

Basics of Regular Expressions in js, regular expressions in js

Definition: regular expressions are used to specify the content retrieved in the text. It is a powerful tool for matching string execution modes.

RegExp (Regular Expression) object Syntax: new RegExp (pattern, attributes );

Pattern is a string or matching rule.

Attributes is an optional string, including attributes g, I, and m

G: indicates global match (continue matching)

I: Indicates case-insensitive matching.

M: Indicates multi-row matching. It is only useful for the ^ and $ modes.

Regular Expressions can be defined in two forms:

Constructor: new RegExp (pattern, attributes); example: var reg = new RegExp ("abc", "g "); the regular expression in the constructor can be a constant string or a js variable.

Normal Mode:/pattern/attributes (/regular/attribute) Example: var reg =/abc/g; regular mode must be a constant string

Each regular expression has a lastIndex attribute, which is used to record the position (index value) of the last match ).

Obtain reg. lastIndex and set reg. lastIndex = 0;

Example: var str = 'abcdefg ';

Var reg = new RegExp ('AB', 'G ');

Alert (reg. test (str ));

Alert (reg. lastIndex); // 2

Metacharacters:

\ D: match any number, 0 ~ Any one of the 9

\ S: matches any blank character

\ B: match a word boundary. It does not match any characters.

\ W: match any character (letters, numbers, and underscores).: match any character, except for line breaks (\ n)

\ N: Find the line break

\ U: usually used to match Chinese Characters

The expression "\ d", "\ s", "\ B", "\ w". The corresponding uppercase letter indicates the opposite meaning, for example: \ D matches all non-numeric characters.

Square brackets:

[Abc]: searches for any character between square brackets.

[^ Abc]: searches for any character that is not in square brackets.

[0-9]: searches for any number ranging from 0 to 9.

[A-z]: searches for any characters from lowercase to lowercase.

[A-Z]: Find any character from uppercase A to uppercase Z.

Quantifiers:

N +: match any string containing at least one n.

N? : Match any string containing zero or one n.

N *: match any string containing zero or more n characters.

N {X}: string that matches X n sequences.

N {X, Y}: string that matches the sequence of X to Y n.

^ N: match any string starting with n.

N $: match any string ending with n.

Others:

|: Match the Left or Right

\: Special punctuation marks of the escape character. After \ is added, it indicates the character itself.

^ To match the character "^", use \ ^

() To match the parentheses themselves, use \ (and \) Other special Punctuation Marks [] {}.? + * |

Unicode-encoded hexadecimal UTF-8 Chinese character encoding: 4e00 minimum Chinese Character 9fa5 maximum Chinese Character

/^ [\ U4e00-\ u9fa5] + $ // common Chinese character encoding range

RegExp object Method

Test: returns true or false reg. test (str) for the specified value in the search string );

Exec: retrieves the string and returns the first value of the search result, reg.exe c (str );

Compile: This method can re-edit the specified Regular Expression var reg = new RegExp ("13 [4-9] (\ d) {8}", "g ");

// Re-edit the regular expression reg. compile ("13 [0-3] (\ d) {8}", "g ");

String object Method

Search: Use str. search (reg); (Return Index value, no-1)

Match: returns matching of all regular expressions (plus g) usage: str. match (reg); (returns an array)

Replace: replace the substring that matches the regular expression. Usage: str. replace (reg ,"");

Split: splits (removes) matching strings. Usage: str. split (reg); (returns an array)

<! Doctype html> 

 

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.