Day17-javascript Regular Expressions

Source: Internet
Author: User

I. Summary

In Python learning we also learned a re module, in JavaScript is also supported regular, JS is relatively simple only two methods:

Test-Checks whether the string matches the regular match and returns True,false

EXEC-Gets the contents of the regular expression match, if it does not match, the value is null, otherwise, gets the array that matches successfully

Second, the regular review
11. Code2 match any one of the characters in the square buckle number,3Where "-" can be used to indicate the scope of the development [0-9][a-z,a-Z]4         5 Example:6/[012] can match the characters in the 0A1B2C, 0, or.7 the quantifier {}8     9[^...] No, except for the bracketed contents, the other can matchTen  One[^a-z], except the lowercase letter A-z mismatch, other matches A          - . Matches any character except line break, carriage return -[^\ r \ n] the          -\d matches any number equivalent to [0-9] -\d matches in addition to numbers, equivalent to [^0-9] -  + \s matches any whitespace character, such as spaces, tabs, line breaks, - equivalent to [\t\n\x0b\f\r] +          A \s relative \s matches non-whitespace characters at  - \w matches any number, letter, Case -[a-za-z0-9_] -          -2. quantifier -  in ? Match the previous item 0 or 1 times -  toExample:/js?/acting on S +/[js]?/Action on JS -+match the preceding item 1 or more times, the single at least once *              $*match a previous item 0 times, or multiple timesPanax Notoginseng any number of times -              the{n} matches two times/o{2}/Google, matching contacts two O home mismatch +                  A{N,m} match count Range n<=x<m the                  +                  -3, Location $  $^ The beginning of the character, pay attention to it [^...] -  - Example: the/^g/match good, mismatch bug -         Wuyi $ match string end of line the/g$/match bug mismatch good -          Wu          - \b Match the boundaries of a word AboutExample:/e\b/match I Love seek, match E in love $          - \b, in contrast to \b, matches the middle -  -  A4, select Match | or +Example/1|2/match 1, or 2 the          -5, grouping () $  theExample, JavaScript,/j (AVA)? Script the      the can match jsctipt, JavaScript the          - Note the distinction [AVA] is equivalent to 3 units, as long as there is a match on the line in(AVA) is a finishing, (AVA)? Match Ava once or 0 times

Third, the regular in JS

1. Create by using the RegExp class constructor method
\D{3}
Syntax format: New REGEXP (Pattern,[flags])
Pattern: Required parameter that specifies the flag information for the regular expression.
G: Global flag, if the flag is set, for MO text to perform a search, or replace
All the matches in the text will function if they are not applied only once.
I: Ignore case flags,
M: Multiline flag, if this flag is not set, then ^ can only match the beginning of the string, and $ can only match the end of the string.

var objexp=new RegExp ("\\d", g);


2. Create a RegExp object with regular expression literals

Regular expression literals consist of two//middle join pattern matching strings
If you also want to specify the flag information, then/plus the flag information on the last side, such as G,i
var objexp=/\d/g;


3. Using the test () method for pattern matching
The test () AA method is used to match a specified character to an execution pattern,
Returns true if the match succeeds, otherwise false

  

 Check the phone/^ (\d{3,4}-)? \d{8}$//^ (\d{3}-)? \d{8}$|^ (\d{4}-)? \d{7,8}$<! DOCTYPE html>

4. Using the Exec () method for pattern matching

The Exec () method return value is not true, false,

Instead, NULL is returned when no matching characters are found.
Otherwise an array is returned with the first element of the array containing a string that matches the regular expression
Other elements contain matching individual groupings ()

Regexp.exec (str)

example, ID card number, extraction date of birth

  

1<! DOCTYPE html>234<meta charset= "UTF-8" >5<title>Title</title>67<body>8<form name= "Form1" >9<div>pls Input: Zoning card number </div>Ten<input type= "text" name= "Idcard" id= "Idcard" > One  A<input type= "button" value= "Extract Birth date" onclick= "Getbrithday ()" > -</form> -  the<script> -         functionGetbrithday () { -             varStr=Form1.idcard.value; - Console.log (str); +             varobjexp=/(\d{6}) (\d{8}) (\d{4})/; - Console.log (Objexp.test (str)); +             varArr=objexp.exec (str); A             if(arr!=NULL){ atAlert ("Date of birth:" +arr[2]); -}Else{ -Alert ("Gun") -             } -         } -</script> in</body> -

Iv. related methods in strings
12345678 obj.search(regexp)                   获取索引位置,搜索整个字符串,返回匹配成功的第一个位置(g模式无效)obj.match(regexp)                    获取匹配内容,搜索整个字符串,获取找到第一个匹配内容,如果正则是g模式找到全部obj.replace(regexp, replacement)     替换匹配替换,正则中有g则替换所有,否则只替换第一个匹配项,                                        $数字:匹配的第n个组内容;                                        $&:当前匹配的内容;                                        $`:位于匹配子串左侧的文本;                                        $‘:位于匹配子串右侧的文本                                        $$:直接量$符号
v. Custom form validation plug-in

5.1. What is the purpose of the form verification?

Reduce requests to the database, the simple user input format or the user input is empty, but the password and the user's operation, need to hand over to the background processing. But if the browser's JS is disabled, it can only be sent to the background, let the background to deal with. So we usually write a part of the front end, the back end is all written.

Day17-javascript 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.