JS Regular expression

Source: Internet
Author: User

Regular expressions

Regular expressions, also known as regular expressions. (English: Regular Expression, often abbreviated as regex, RegExp, or re in code)

Create a RegExp object

The RegExp object is used to store the retrieval mode.

var patt1=new RegExp("e","g");//创建对象定义,检索模式为e(查找e这个字符)var patt1=/e/g;//直接量定义,检索模式为e(查找e这个字符)
Methods for REGEXP objects

The RegExp object has 3 methods: Test (), exec (), and compile ().

    1. Test (): Retrieves the specified value in the string. The return value is the value that was found. If no match is found, NULL is returned.

    2. EXEC (): Retrieves the specified value in the string. The return value is the value that was found. If no match is found, NULL is returned. (exec stores the matched bits on a global match, and executes again from the stored position)

    3. Compile (): Changing the retrieval mode of RegExp

      //先查找e,再查找d。因为这个字符串中存在e,不存在d所以返回truefalse//查找evar patt1=new RegExp("e");document.write(patt1.test("The best things in life are free"));//查找dpatt1.compile("d");document.write(patt1.test("The best things in life are free"));
RegExp Object Properties
Properties Description FF IE
Global RegExp whether the object has a flag g. 1 4
IgnoreCase RegExp whether the object has a flag I. 1 4
LastIndex An integer that indicates the character position at which to start the next match. 1 4
Multiline RegExp whether the object has a flag m. 1 4
Source The source text of the regular expression. 1 4
Methods for String objects that support regular expressions
Method Description FF IE
Search Retrieves a value that matches a regular expression. 1 4
Match Finds a match for one or more regular expressions. 1 4
Replace Replace the substring that matches the regular expression. 1 4
Split Splits a string into an array of strings. 1 4
Modifier
modifier Description
I Performs a match that is not case sensitive.
G Performs a global match (finds all matches rather than stops after the first match is found).
M Performs multi-line matching.
Square brackets (used to find characters in a range)
An expression Description
[ABC] Look for any characters between square brackets.
[^ABC] Look for any characters that are not in square brackets.
[0-9] Look for any number from 0 to 9.
[A-z] Finds any characters from a to lowercase z from a small write.
[A-z] Look for any characters from uppercase A to uppercase Z.
[A-z] Look for any characters from uppercase A to lowercase z.
[ADGK] Finds any character within a given collection.
[^ADGK] Finds any character outside the given collection.
(Red|blue|green) Finds any of the specified options.
Metacharacters (Metacharacter, a character with a special meaning
Meta character Description
. Finds a single character, in addition to line breaks and line terminators.
\w Find the word character.
\w Finds non-word characters.
\d Find numbers.
\d Finds non-numeric characters.
\s Find white space characters.
\s Finds non-whitespace characters.
\b Matches the word boundary.
\b Matches a non-word boundary.
/ Find NUL characters.
\ n Find line breaks.
\f Find a page break.
\ r Look for the carriage return character.
\ t Find tabs.
\v Find vertical tabs.
\xxx Find the characters that are specified in octal number XXX.
\xdd Finds the characters specified in hexadecimal number DD.
\uxxxx Finds the Unicode characters specified in hexadecimal number xxxx.
Quantifiers
quantifier Description
n+ Matches any string that contains at least one n.
N Matches any string that contains 0 or more N.
N? Matches any string that contains 0 or one n.
N{X} Matches a string that contains a sequence of X N.
N{x,y} Matches a string containing a sequence of X to 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.
Capture
Capture Description
(x) Match ' X ' and remember the match.
(?: x) Matches the specified string x but does not remember the match (the parentheses are not used as an item in the result array).
<!DOCTYPE html><script>var test = ‘this are some test text‘;console.log(test.match(/(te)(?:st)/));//[ ‘test‘, ‘te‘, index: 14, input: ‘this are some test text‘ ]//(te)被捕捉了(结果数组第二项:test中的te被捕获),(?:st)没有被捕捉(结果数组中没有)//</script>
Pre-check

| Pre-check | Description |
| (? =x) | Matches any string immediately after the specified string x (positive lookup). |
| (?! X) | Matches any string that is not followed by the specified string x (forward negation lookup). |

Looks like ES7 to support reverse pre-check , first use parentheses instead of it

<!DOCTYPE html><script>var test = ‘this are some test text‘;console.log(test.match(/te(st.*)/));console.log(‘反向预查:‘+test.match(/te(st.*)/)[1]);/*[ ‘test text‘,  ‘st text‘,  index: 14,  input: ‘this are some test text‘ ]反向预查:st text*/</script>

Positive pre-check usage: pre-check the end of the conditions, pre-check the conditions are not included in the matching results, pre-check conditions do not affect the current matching position pointer
Eg: Find out all the

<!DOCTYPE html>
Match Results
Properties meaning
$1...$9 If it (s) exists, is the substring that matches to
$_ The string that is matched
$* Whether to make multi-line matching (bool type)
$& Last-match Index
$+ The last substring enclosed in parentheses
$` Last match with left substring
$ Last match with right substring
var test = ‘this are some test text‘;document.write(test.replace(/(this).*?(text)/, "\$1 = $1,\$2 = $2,\$_ = $_,\$* = $*,\$& = $&,\$+ = $+,\$` = $`,\$‘ = $‘"));

JS Regular expression

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.