1. Regular concepts
What is the purpose of the birth of JS?
It is to make form validation.
Before JS does not appear, verify forms to be transferred to the background, so do the background after the data verification process, and then return to the front page processing results. In limited bandwidth, the entire verification process is very complicated and time-consuming.
In use JS to do the verification process, implement a simple validation rule is very troublesome.
Such as: Get number string from the clutter
var str = "asd68asd687as6da78sd123123zcacas" var linshi=''; var arr=[] for(var i = 0 ; i < str .length ; i ++){ if(str.charAt(i)>=0&&str.charAt(i)<=9){ linshi+=str.charAt(i) }else{ if(linshi!=''){ arr.push(linshi) } } } if(linshi!=''){ arr.push(linshi) } console.log(arr)
If you use a regular way:
var arr = str.match(/\d+/g);
On it
What are regular?
Regex (regular expression) is an object-character description of the rule. It can be used to check whether a character string containing a character to be replaced or removed matches a substring from a string of conditions and the like.
Regular Expressions:
Regular expression is actually a rule, in fact, the rules of regular expressions referred to more appropriate. Regular grammar based on an ancient perl language.
Why use regular:
The front tend to have a lot of form data verification work, will make use of regular expressions greatly reduce the workload of data validation. Common effects: email, phone number, ID number, etc. (the case of skilled)
Regular use of the concept:
Any program can not write regular expressions, but according to the principle of lazy, how to do self-evident.
2. Create the way
Create a regular way:
1. constructor argument
var REG1 = new new the RegExp ( "a" ); var STR = "adassdfsd" ; the console.log (str.match (REG1)) // output matching a character
2. literal way, old style perl language
var REG2 = / a / ; the console.log (str2.match (REG2)); // the output of a matching character
/ / Is a regular expression identifier
"" Is a string identifier
[] Is an array of identifiers
{} Is the identifier of the object
The modifier regular, regular expression written on the back behind / in:
g represents global matching, find all
i indicates ignore case
3. E-mail rules
Numbers + English + @ + English +. + English
Regular verification:
var reg = / [0-9] /;
reg.test (str) // returns true if the verification is successful, false indicates failure
4. String .replace (oldstr, newstr) function and string .match (regular) function
Function string replace function: to replace the old string to the new string
This function can be used, the old string represented as a regular expression, replacement string
Such as: string abc replaced by "ha", if not followed by the regular g, only a replacement, if not i, the caps will not be replaced
var str = “abc123abc456abbcdABCefaacbcdbcabc"; var reg = /abc/g; console.log(str.replace(reg,"哈哈"));
Function string match function: extract all the things you want
It returns an array containing all eligible characters
\ D matches all numbers
var REG = / \ D / G; the console.log (str.match (REG)) // print string all numbers
+ Number, quantifier, indicates how many can, at least one uncapped
var REG = / \ + D / G; the console.log (str.match (REG)) // print string all numbers, continuous
| Or, like js in ||
var REG = / ABC | BBC | CBC | DBC / G the console.log (str.match (REG)); // print string ABC, BBC, CBC, DBC var REG = / [AC] BC / G
[] Symbol Meta
var reg = / [az] / // matches all the letters
[0-9] == \d
^ Excluded (except)
var reg = / [^ 0-9] / // represents all numbers except
Representatives of all, is not recommended
var REG = /<.+>/ G console.log (str.replace (REG, "")) // filter, angle brackets represent things inside do not, however?
Regular greed Law:
var REG = / <[^ <>] +> / G the console.log (str.replace (REG, "")) // represents the angle brackets do something really
Escape character:
\ D - [0-9] Digital
\ W - [a-z0-9_] numbers, letters, underline
\ S - blank characters (spaces)
\ D - [^ 0-9] non-numeric
\ W - [^ a-z0-9_] non-numeric, alphabetic, underscore
\ S - non-whitespace characters
Quantifier: qualifier, you can specify a regular expression of a given group, how many times must occur to meet the match
* Matches the preceding subexpression zero or more times
+ Preceding subexpression matches at least one or more times
? Matches the preceding sub-expression zero or one time
Matching determining {n} n times
{N,} matches at least n times
{N, m} matches at least n times, matching up m times
Note: After the qualifier followed? , Matched by greed become non-greedy match
Qq number validation rules:
var reg = / [1-9] \ d {} 4.10 of the /
Cured rules: 0411-6666888
Beginning with 0 or 2 or number plus three - eight digits begins with non-zero, the end of the extension is 1 to 4
var reg = /(0\d{2,3}-)?[1-9]\d{7}(-\d{1,4})?/
Complex rules mailbox: alphanumeric string of English underscore @ or the number of sub string of English.
var reg = waz09azi
I want to check how to do? Before the regular escape character plus \
Regular way
Regular .test (string), the return value is true and false
Regular .test (string) has a characteristic, that is, as long as part of the string to meet the requirements, it will return true
Solution:
^ Start
$ End
exec () returns an array of find, can not find in return null
Remove the extra space
str.replace(/\s+/g,'');
Delete trailing spaces
str.replace(/^\s+/,'');
str.replace(/\s+$/,'');
Check the 6-digit postal code, the first can not be 0
/^[1-9]\d{5}$/
Regular Expression Manual
Commonly used detection method:
1. Chinese Detection
unicode encoded Chinese surveillance: / ^ [\ u2E80- \ u9FFF] + $ /
2. User name detection
Regular: / ^ [a-z0-9 _-] {3,16} $ /
3. mail detection
/^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/
It can appear multiple times (underlined alphanumeric .-) may occur multiple @ (.- alphanumeric). 2-6 or letters.
/^[a-z\d]+(\.[a-z\d]+)*@([\da-z](-[\da-z])?)+(\.{1,2}[a-z]+)+$/
There may be multiple (alphanumeric) may have a plurality of free (plurality (alphanumeric)) may be a plurality @ (alphanumeric there may be no 1 (- alphanumeric)) can have multiple (2 or 1 a plurality of letters)
4.URL detection
/^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w\.-]*)*\/?$/
There are 1 or 0 times (http times with a 1 or 0 s: //) may be a plurality (alphanumeric .-) 2-6 (letters.) May be 0 or more (or a plurality may be 0. / .- alphanumeric underlined) may be 0 or 1 /
5.HTML label inspection
/^<([a-z]+)([^<]+)*(?:>(.*)<\/\1>|\s+\/>)$/
<Plurality (letters), or may be a plurality of 0 (all the characters except <a) />
<Plurality (letters), or may be a plurality of 0 (all the characters except <a)> any number of characters </ repetition of the first portion of the plurality of letters>
Since the html tag definition
/<[^<>]+>/g