JavaScript---Regular type (i)

Source: Internet
Author: User

Regular Expressions (regular expression) is an object that describes the character pattern . used to verify that the client's input data conforms to established rules , If we restrict the input character length or format, etc. . because client authentication , can save a lot of server-side system resources , and provide a better user experience .

① Creating regular expressions

two ways to create :

var box = newregexp (' box ', ' IG ');//first parameter optional string, second parameter pattern modifier var box =/box/ig;//directly with two backslashes

② Testing Regular Expressions

Test pattern match in string, return true or falsevar pattern =/box/i;var str = ' box '; alert (Pattern.test (str)); Use exec to return the matching array var pattern =/box/i;var str = ' box '; alert (pattern.exec (str));

③ Regular expression methods that use strings

/* Use the match method to get the matching array */var pattern =/box/ig;//Global search var str = ' This is abox! That's a box too! '; Alert (Str.match (pattern));//box,box, matching to two Boxalert (Str.match (pattern). length);//2, get array lengths/* Use Search to find matching data */ var pattern =/box/ig;//Global case-insensitive search. Notice that the search method finds the                       return, which means that G is dispensable. var str = ' This is abox! That's a box too! '; Alert (Str.search (pattern));//10, find the return position, or return-1/* Replace the matching data with replace */var pattern =/box/ig;var str = ' This is abox! That's a box too! Alert (Str.replace (pattern, Tom));//Replace Box with Tom


Run Results :

/* Use Split to split the string array */var pattern=//ig;var str = ' This is abox! That's a box too! '; Alert (str.split (pattern));//divide spaces into arrays

Run Results :

/* Use static property */var pattern =/(g) oogle/;var str = ' this isgoogle! '; Pattern.test (str); alert (regexp.input);//thisis Google!alert (Regexp.leftcontext);//thisisalert ( Regexp.rightcontext);//!alert (Regexp.lastmatch);//googlealert (Regexp.lastparen);//galert (RegExp.multiline);// False,ie is undefined.


/* Use dot character */varpattern =/g...gle/;//... Match three arbitrary characters var str = ' gooogle '; alert (Pattern.test (str));//true/* Use character class to match */var pattern =/g[a-za-z]*gle/;//[a-z]* Represents any of the characters in A-Z, is not case-sensitive, you can try to remove the changes in the [] see the effect var str = ' Google '; alert (Pattern.test (str));//true Varpattern =/g[^0-9]*gle /;//[^0-9]* denotes any non-0~9 character var str = ' go0gle '; alert (Pattern.test (str));//false var pattern =/[a-z][a-z]/;//[a-z] or [A-z] Any number of characters at the end with or without "+" no effect var str = ' GOOGLE '; alert (Pattern.test (str));//true/* Use meta-symbol to match */var pattern =/g\w*gle/;//\w* Matches any number of all alphanumeric and _var str = ' goo_gle '; alert (Pattern.test (str));//true var pattern =/\d{7,}/;//\d{7,} matches at least 7 non-numeric var str = ' Googlej '; alert (Pattern.test (str));//true


/* Use the anchor element character match */var pattern =/^google$/;//^ from scratch to match, starting at the end of the match var str = ' Google '; alert (Pattern.test (str));//true

/* Repeat match */var pattern =/g*gle/;//match 0~n var str = ' Google ',//*,?, +,{n,m}alert (pattern.test (str));//true

/* Use or pattern match */var pattern =/google|baidu|bing/;//google|baidu|bing three kinds of one of the string var str = ' Google '; alert (Pattern.test (str)) ;//true


JavaScript---Regular type (i)

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.