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 expressions?
The reason we study the regular expression is very simple, the purpose is to help us to quickly match the string.
We can use the following code to simply taste:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <Title> using regular expressions reason </ title> </head> <body> </body> <script> // For example, we now have such a demand, a digital string out among var STR = "12 is, 90, ABC, ADMIN-root30369, ....." ; // Create an empty array var ARR = []; var tmp = '' ; // Open loop processing data for ( var I = 0; I <str.length; I ++ ) { // determines whether the contents of the string in a digital IF (str.charAt (I)> = '0' && str.charAt (I) <= '. 9' ) { tmp += str.charAt(i); }else{ if(tmp){ arr.push(tmp); tmp = ''; } } } // print the console.log (ARR); </ Script> </html>
Regular way look:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <Title> by a positive number among the string is taken </ title> </head> <body> </body> <script> var str = "12 ,90,abc,admin-root30369,....."; var n = /\d+/g; console.log(str.match(n)); </script> </html>
emmmmmmmmmmmmmmmm, so you say Well who use
How to create a regular expression
<Script> // three ways to define a regular expression: var RE1 = / ABC /; // define the regular easiest way, directly from a direct amount which is in the form of a regular two slashes content. var RE2 = new new the RegExp (/ ABC /); // define the second form of regular, may be changed to the following wording var re2_two = new new the RegExp ( "ABC"); // equivalent to the above wording var RE3 the RegExp = ( "Hello"); // use positive transfer function is created </script>
In the above code, we have three forms were created by a regular expression, no matter from which side, it is recommended to use the first wording.
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;
console.log (str.match (reg)) // print string all digital
+ Number, quantifier, indicates how many can, at least one uncapped
var reg = /\d+/g;
console.log (str.match (reg)) // print string all numbers, continuous
| Or, like js in ||
var reg = /abc|bbc|cbc|dbc/g
console.log (str.match (reg)); // print string abc, bbc, cbc, dbc
var reg = /[a-c]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
was reg = /<.+>/g
console.log (str.replace (reg, "")) // filter, angle brackets represent things inside do not, however?
Regular greed Law:
var reg = /<[^<>]+>/g
console.log (str.replace (reg, "")) // inside angle brackets denote things do not 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
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
// use regular: // by using the method // Regular itself: // reg.test (str); verification // reg.exec (str); Find // characters method: // str.match (REG) ; Find // str.replace (REG, newStr); replacement // str.search (REG) Find
exec () returns an array of find, can not find in return null
Characteristics of test: verification, validation locally, as long as a certain part of verification of compliance with the regular characters are true
// QQ number: Tencent // var STR = "704 206 198"; // var REG = / ^ [1-9] \ {D} $ 5,11 /; // the console.log (reg.test (STR)) ; // ZIP // var STR = "152 100"; // var REG = / ^ \. 6 {D} $ /; // the console.log (reg.test (STR)); // 固定电话 // var str = "0504-59271632-123"; // var str = "0504-59271632"; // var str = "59721632"; // var str = "59721632-123"; // var reg = /^(0\d{2,3}-)?[1-9]\d{6,7}(-\d{1,4})?$/; // console.log(reg.test(str)); // complex mailbox // var STR = "[email protected]"; // . Var REG = / ^ \ W @ {1,10} [0-9a-Z] {2,10} (\ [ AZ] {2,3}) $ {1,2} /; // the console.log (reg.test (STR)); // URL // var str = "https://baidu.com"; // var str = "http://www.baidu.com"; // var str = "www.baidu.com"; // var str = "mp3.baidu.com"; // var str = "baidu.com.cn"; // var reg = /^(https?:\/\/)?([0-9a-z]{1,10}\.)?[0-9a-z]{2,10}(\.[a-z]{2,3}){1,2}$/; // console.log(reg.test(str));
Regular Expression Manual
Conventional detection methods:
1 . Chinese detect unicode encoded Chinese surveillance: / ^ [\ u2E80- \ u9FFF] + $ / 2 . Username detection Regular: / ^ [A-Z0-9 _-] {3,16} $ / 3 . Mail detection /^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([az\.]{2,6})$/ can be more (letters underlined numbers. occurrences -) @ a plurality may occur (alphanumeric .- .) 0.2 and 6 letters or / ^ [az \ d] + (. \ [az \ d] +) * @ ([ \ da-z] (- [ \ da-z])?.) + (\ {1,2} [az] +) + $ / may have a plurality of (alphanumeric) may have a plurality of free (more. a (alphanumeric)) may be a plurality @ (alphanumeric there may be no 1 ( - alphanumeric)) may have a plurality (one or more than two letters). [email protected] . 4 .URL detection / ^ (https:? \ / \ /)?. ([\ Da-z \ .-] +) \ ([. Az \] {2,6}) ([\ / \ w \. ? -] *) * \ / $ / have 1 or 0 times (http there once or twice 0 S: // ) may be more (alphanumeric .-) 2-6 (letters) can be more. or 0 (or a plurality may be 0 / alphanumeric .- underlined) may be 0 or 1 / HTTP: // sh.op-ta.l.baidu.com . 5 .HTML tag detection /^<([az]+)([^<]+)*(?:>(.*)<\/\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