Summary of JS Regular expressions

Source: Internet
Author: User

New Regular expression

var reg =/Regular expression/(g,i,m)

var reg = REGEXP (' Mutations ', (G,I,M))

The G Global match does not represent a match only to the first

I ignore case

M multi-line matching

1. Verification

When used for validation, it is often necessary to add ^ and $ respectively to match the entire string to be validated;

2. Search and replace

To add \b to the front and back

Character class matching

[...] Find any character between square brackets
[^ ...] Find any characters that are not in square brackets
[A-z] find any characters from small to lowercase Z
[A-z] find any characters from uppercase A to uppercase Z
[A-z] find any characters from uppercase A to lowercase Z
. Find a single character, except line breaks and line terminators
\w find word characters, equivalent to [a-za-z0-9]
\w Find non-word characters, equivalent to [^a-za-z0-9]
\s finding whitespace characters
\s finding non-whitespace characters
\d find number, equivalent to [0-9]
\d Find non-numeric characters, equivalent to [^0-9]
\b Match word boundaries
\ r Find Carriage return character
\ t find a tab
To find a NULL character
\ nto find line breaks

repeating character matching

{N,m} matches the previous item at least n times, but cannot exceed m times
{N,} matches the previous item n or more times
{n} matches the previous item n times
N? Matches the previous item 0 or 1 times, which means the previous item is optional, equivalent to {0,1}
n+ matches the previous item 1 or more times, equivalent to {1,}
n matches the previous item 0 or more times, equivalent to {0,}
n$ matches any string ending with n
^n matches any string starting with N
=n matches any string immediately followed by the specified string n
?! n matches any string that is not followed by the specified string n

' abc123 '. Replace (/\w (? =\d)/, ' 123 ')"ab123123"
' abc123 '. Replace (/\w (?! \d)/, ' 123 ') "123bc123"

  

Match a specific number

^[1-9]\d*$ matching positive integers
^-[1-9]\d*$ matching negative integers
^-? [0-9]\d*$ Match integer
^[1-9]\d*|0$ matches non-negative integers (positive integers + 0)
^-[1-9]\d*|0$ matches a non-positive integer (negative integer + 0)
^[1-9]\d*\.\d*|0\.\d*[1-9]\d*$ matching a positive floating point number
^-([1-9]\d*\.\d*|0\.\d*[1-9]\d*) $ matches negative floating-point number
^-? ([1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0) $ match floating point number
^[1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0$ matched non-negative floating-point number (positive floating point + 0)
^ (-([1-9]\d*\.\d*|0\.\d*[1-9]\d*)) |0?\.0+|0$ matches non-positive floating-point number (negative floating-point number + 0)

Match a specific string

^[a-za-z]+$ matches a string consisting of 26 English letters
^[a-z]+$ matches a string of 26 English letters in uppercase
^[a-z]+$ matches a string of 26 English letters in lowercase
^[a-za-z0-9]+$ matches a string consisting of a number and 26 English letters
^\w+$ matches a string of numbers, 26 English letters, or underscores

Group () () () $1$2$3

Test Reg.test ()

var reg =/abc/g;    var str = "123ABC456ABC";    Console.log (Reg.lastindex);//0    Console.log (reg.test (str));//true    Console.log (reg.lastindex);//6    Console.log (Reg.test (str));//true    Console.log (reg.lastindex);//12    Console.log (reg.test (str));//false

Reg.exec ()

var str = "xyz";  var reg1 =/x/;  var reg2 =/a/;  var res1 = reg1.exec (str);  var res2 = reg2.exec (str);  Console.log (res1);//["X", index:0, Input: "xyz"]  Console.log (res2);//null

Reg.search ()

var str = "ABCDCEF";  Console.log (Str.search (/c/g));//2

  

Summary of JS Regular expressions

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.