Javascript Regular Expression notes

Source: Internet
Author: User

 

Special Character escape (lower case !)

 

\ W word letter and digit underline [a-zA-Z0-9 _]

\ S space any Unicode white space character [\ N \ r \ f \ t \ v]

\ D decimal [0-9]

\ B bound word boundary (/^ JavaScript \ B/matches "javascript is...", not "javascript: Alert)

 

Repeated

 

{N}

{M, n}

{M,} repeated times greater than m

 

Group and reference

 

Use parentheses to group regular expressions.

Group has two roles: Repeat, but reference.

\ $

 

Matching position

 

(? =) Pre-query specifies the matching string to be matched next

For example:/JavaScript (? = Hello)/match the javascript string in "javascripthelloworld"

 

(?! ) Pre-check non-

 

(? :) Matched but not obtained

 

Flag

 

M multi-row mode: If the match is not added, only the result of a single row is matched. ^ the first $ row at the end of the row is matched.

 

Others

 

\ XN matches n, where N is the hexadecimal escape value. The hexadecimal escape value must be determined by the length of two numbers.

 

\ Num matches num, where num is a positive integer. References to the obtained matching.

 

\ N identifies an octal escape value or a backward reference. If at least N subexpressions are obtained before \ n, n is a backward reference. Otherwise, if n is an octal digit (0-7), n is an octal escape value.

 

\ Nm identifies an octal escape value or a backward reference. If there are at least is preceded by at least nm obtained subexpressions before \ nm, then nm is backward reference. If at least N records are obtained before \ nm, n is a backward reference followed by text M. If none of the preceding conditions are met, if n and m are Octal numbers (0-7), \ nm matches the octal escape value nm.

 

\ NML if n is an octal digit (0-3) and both M and l are octal digits (0-7), the octal escape value NML is matched.

 

\ UN matches n, where n is a Unicode character represented by four hexadecimal numbers.

 

Regular Expression matching Chinese characters: [u4e00-u9fa5]

 

Match double byte characters (including Chinese characters): [^ x00-xff]

 

? When this character is followed by any other delimiter (*, + ,?, The matching mode after {n}, {n ,}, {n, m}) is not greedy. The non-Greedy mode matches as few searched strings as possible, while the default greedy mode matches as many searched strings as possible.

 

(? : Pattern) matches pattern but does not get the matching result. That is to say, this is a non-get match and is not stored for future use.

 

You can view the. Source attribute of the regular expression object by checking whether the regular expression is correct (checking whether the escape character is correct.

 

Example Code

 

Code
< Script Language = " Javascript " >
VaR Strsrc =   " Xxa1b01c001yya2b02c002zz " ;
VaR Re =   / A (\ D) B (\ D {2}) C (\ D {3 }) / GI;
VaR Arr, count =   0 ;
While (ARR = Re.exe C (strsrc )) ! =   Null ){
Displayresult ();
}
Function Displayresult (){
Document. Write ( " <P> This is a regular expression/ "   + Re. Source +   " /GI pairs <br> \ "" + Regexp. Input + " \ " Proceed "   + ( ++ Count) +   " Search Results: <br> " );
Document. Write ( " Regexp. index is "   + Regexp. Index +   " <Br> " );
Document. Write ( " Regexp. lastindex is "   + Regexp. lastindex +   " <Br> " );
Document. Write ( " Regexp. lastmatch is "   + Regexp. lastmatch +   " <Br> " );
Document. Write ( " Regexp. lastparen is "   + Regexp. lastparen +   " <Br> " );
Document. Write ( " Regexp. leftcontext is "   + Regexp. leftcontext +   " <Br> " );
Document. Write ( " Regexp. rightcontext is "   + Regexp. rightcontext +   " <Br> " );
Document. Write ( " Regexp. $1 is "   + Regexp. $ 1   +   " <Br> " );
Document. Write ( " Regexp. $2 is "   + Regexp. $ 2   +   " <Br> " );
Document. Write ( " Regexp. $3 is "   + Regexp. $ 3   +   " <Br> " );
Document. Write ( " Regexp. $4 is "   + Regexp. $ 4   +   " <Br> " );
Document. Write ( " Arr. index is "   + Arr. Index +   " <Br> " );
Document. Write ( " Arr. input is "   + Arr. Input +   " <Br> " );
Document. Write ( " Arr. lastindex is "   + Arr. lastindex +   " <Br> " );
Document. Write ( " The number of elements in the returned array is "   + Arr. Length +   " <Br> " );
Document. Write ( " The returned array content is [ " );
For ( VaR I = 0 ; I < Arr. length; I ++ ){
If (I < Arr. Length - 1 )
Document. Write ( " \ "" + Arr [I] + " \ " , " );
Else
Document. Write ( " \ "" + Arr [I] + " \ " ] </P> " );
}
}
< / SCRIPT>
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.