JavaScript Regular Expressions

Source: Internet
Author: User

A regular expression is a description of the structure of a string , which simply means the character arrangement of a string. It is a master in string matching and processing.

The regular notation

1. abbreviation :/pattern/[attributes]

var reg =/abc/;  Literal way

2. Full write : New RegExp (pattern[, Attributes])

var reg = new RegExp ("abc");

Where attributes is any combination of "G" (full-text lookup), "I" (ignoring case), and "M" (Multi-line lookup), the default value is "non".

var reg = new RegExp ("A", "GI");   Match all A or a  

When a regular is a variable, it can only be used in full write mode.

var str = "abc"; var reg =  new RegExp (str);

The regular method

EXEC: Retrieves the value specified in the string. Returns the found value and determines its location.
test: a regular match string that returns a Boolean.
compile : compiles the regular expression.

Methods for String objects that support regular expressions

Search: string match Regular, return position (Failed return: -1), similar to String.IndexOf ()

match: String matches regular, returns array (Failed return: null)

Replace: The string matches the regular, and the matching successful character is replaced with the new string.

split: Splits a string into an array of strings.

Metacharacters

(   [  {  \  ^  < Span class= "Apple-tab-span" > $  |       *   +  

Metacharacters are part of a regular expression and must be escaped when we want to match the regular expression itself .

Pre-defined Classes

. finds a single character, in addition to line breaks and line terminators. (decimal point) matches any single character except for the beginning of a new line.

\w Find word characters: numbers, letters, underscores. equivalent to [a-za-z0-9_].

\w Find non-word characters. equivalent to [^a-za-z0-9_].

\d Find numbers. equivalent to [0-9].

\d to find non-numeric characters. equivalent to [^0-9].

\s Find white space characters. includes spaces, tabs, page breaks, and line breaks.

\s Find non-whitespace characters.

\b Match word boundaries: Start, end, space

\b matches a non-word boundary.

To find the NUL character.

\ nto find newline characters.

\f Find a page break.

\ r to find the carriage return character.

\ t to find a tab.

\v Find vertical tabs.

\xxx finds characters that are specified in octal number XXX.

\XDD finds the characters specified in hexadecimal number DD.

\uxxxx finds Unicode characters that are specified in hexadecimal number xxxx.

\ for subsequent characters that are normally treated as literals, they are escaped as special characters.

Quantifiers

n+ matches any string that contains at least one n. Equivalent to N{1,}

n matches any string that contains 0 or more N. Equivalent to N{0,}

n? Matches any string that contains 0 or one n. Equivalent to n{0,1}

n{x} matches a string containing a sequence of X N.

N{x,y} matches a string containing a sequence of X or Y N.

n{x,} matches a string that contains at least X N of a sequence.

n$ matches any string that ends with N.

^ n matches any string that begins with N. When this character appears in the first character of a character set, it will have a different meaning. For example,/[^a-z\s]/will match ' 3 ' in ' My 3 sisters '

? =n matches any string immediately followed by the specified string n.

?! n matches any string that does not follow the specified string n immediately thereafter.

If '? ' Immediately following any quantifier *, +,?,{} will cause the quantifier to become a non-greedy pattern (the fewest number of matches), Is the opposite of the default greedy mode (the maximum number of matches).
For example, using the /\d+/ non-Global match "123ABC" will return "123", and if you use /\d+?/, it will only match to "1".

Character class
The whole of the brackets [] in the character class represents a character!

[ABC] finds any character between square brackets.

[^ABC] finds any characters that are not in brackets.

[0-9] Find any number from 0 to 9.

[A-z] finds any characters from lowercase a to lower Z.

[A-z] finds any characters from uppercase A to uppercase Z.

[A-z] finds any characters from uppercase A to lowercase z.

[ADGK] finds any character within a given collection.

[^ADGK] finds any character outside the given set.

(Red| Blue| Green) to find any of the specified options.

  

Match subkey

Call the whole of the regular as "mother", and the regular in the first parenthesis from the left as the first child,

The second parenthesis is the second subkey, and so on.

parentheses, which can be used as a memory device in regular expressions. This part of the regular matching character will be remembered and can be exploited later.

The methods to match the subkeys are: Replace, match (regular without G when the subkey is matched , and the first item of the returned array is the regular whole)

Each time a regular match is performed, the RegExp property is reset to the current regular match result , such as regexp.$1 first subkey, Regexp.$9 Nineth subkey.

var re =/(\w+) \s (\w+)/; var str = "John Smith"; var newstr = str.replace (Re, "$ $"); Console.log (NEWSTR); Output:smith John

Matching rules

(x) match ' x ' and remember the match . This is called the capture bracket . For example,/(foo)/Match and remember ' foo ' in ' foo bar '. The match to a substring can be accessed through the [1],..., [n] element of the resulting array.

(?: x) matches ' x ' but does not remember matches. This is called a non-capturing parenthesis . The substring matched to cannot be accessed by the [1],..., [n] of the resulting array.

X (? =y) matches ' x ' only when ' x ' follows ' Y '. This is called positive lookup . For example,/jack (? =sprat)/Will match to ' Jack ' only when it follows ' Sprat ':

/jack (=sprat)/.exec ("Jacksprat"); Output: ["Jack"]

X (?! Y) match ' x ' only when ' X ' is not followed by ' Y ', this is called positive negation lookup . For example,/\d+ (?! \.) /Match a number only when the number is not followed by a decimal point.

/\d+ (?! \.) /.exec ("3.141");//output: ["141"] instead of 3.141

Regular application

New RegExp ("\ \s|^" + ClassName + "(\ \s|$)"): The style name of the matching element

Replace (/(\d{3})\d{3,4}(\d*)/, "$* * * "): replace the middle 3-4 digits of the phone number with an asterisk , which supports the 6+-bit mobile phone number.

"13412345678". Replace (/(\d{3}) \d{3,4} (\d*)/, "$1****$2");//output:134****5678

/[+-]? (?:\d*\.|) \d+(?: [ee][+-]?\d+|) /: match numbers (see 67 lines of jquery2.0.3 Source)

Used for matching numberscore_pnum =/[+-]? (?:\ d*\.|) \d+ (?: [ee][+-]?\d+|) /.source,

The vertical bar in this regular | , for the first time looked a little puzzled, because the right side is empty, that is to say what is not, that is nothing.
In fact, that is to say: either has, or does not mean, then use question marks to express should be easier to understand. The Great Gods write code is not the same, hehe ~
So, write the following form, perhaps better understand:

/[+-]? \d* \.? \d+ (?: [ee][+-]?\d+)?/I

Summarize
Personal feeling regular expression beginner, the most easy to be confused by the combination of those symbols. So it's important to figure out what those symbols mean.
And when you see a long regular expression, don't worry, try to conquer with a split method. Do not understand the meaning of the symbol, timely check the information until it is understood.

JavaScript Regular Expressions

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.