Introduction to Regular expressions
set of Rule Expressions
1. Application Scenarios for regular expressions
Form Validation
parsing URL parameters
Filter ....
2. Definition of regular expressions
literal form === " //
constructor Form === " new RegExp ()
difference
cannot put variables in literal form | function
variables can be placed in the form of constructors | function
/* var reg =/a/;alert (reg); */ var New REGEXP ("B"); alert (reg);
View Code
3. Methods of Regular expressions
Test :
Function: see if the content matches the regular
return : boolean value
match = ="true "
does not match ===>false
exec
Function: see if the content matches the regular
Return: array of matched contents
Array of match = =
do not match = ="null "
/* var reg =/a/;var str = "a"; Alert ( reg.test (str)); Truealert (typeof reg.exec (str)); Object Delete typeof returns a*/var num1 =1 ; // var reg1 =/"+num1+"/; var New RegExp (NUM1); var str1 = "1"; // alert (REG1);//"+num1+"/ Alert (Reg1.test (STR1) //true
View Code
Second, the meta-character of the regular expression
"= = ="matches any "one" character in parentheses
[^] = = =matches any character except in parentheses
[0-9] = = =matches any number from 0 to 9
[A-z] = = =matches Any lowercase letter a to z
[A-z] = = =matches any uppercase letter a to Z
\d = = =number of matches:[0-9]
\d = = ="matches non-digital
\w = = ="matches numbers, letters, underscores : [0-9a-za-z_]
\w = = =matches non-digits, letters, underscores
\s = = ="Match spaces
\s = = ="non-whitespace"
\b = = ="Match boundary"
. = = =Any character other than line break
Escape character
\ = = Removes the special meaning of the metacharacters function, such as \d's matching number function \\d added escape after it becomes true D and loses the matching number function
/*var reg =/[abc]/;//matches any one of the characters within the brackets var str = "C"; Alert (Reg.test (str)); True*//*var reg1 =/[^abc]/;var str1 = "U"; alert (Reg1.test (STR1));//true*//*var reg2 =/[3-9]/;var num2 = 5;alert (Reg2.test (num2));//true*///var reg3 =/[a-z]/;/*var reg3 =/[a-z]/;var Str3 = "A"; Alert (Reg3.test (STR3));//true*///var reg4 =/[0-9a-za-z]/; Numeric Case Letters//var reg1 =/\d/;/*var reg1 =/\d/;var Num1 = 5;alert (Reg1.test (NUM1));*///var reg2 =/\w/;/*var reg2 =/\w/;var str2 = "_"; Alert (Reg2.test (STR2));*///var reg3 =/\s/;/*var reg3 =/\s/;var Str3 = ""; Alert (Reg3.test (STR3));*//*var reg4 =/\ba/;var STR4 = "a"; Alert (Reg4.test (STR4));*//*var reg5 =/./;var STR5 = "\ n"; Alert (Reg5.test (STR5));*/varReg6 =/\./;//there is a special meaning, any character other than the line breakvarSTR6 = "."; alert (Reg6.test (STR6));
View Code
Third, regular expression qualifier
^ = = = Startswith the string ( start )
$= =to end of string ( end )
/* var reg =/\d/;// \d ====> number no qualifier num, as long as there is a number can be var num = "5a"; alert (num); */ /* var reg1 =/\w/;// alphanumeric underscore var str1 = "^_^"; Alert ( reg1.test (str1)); */ /* var reg2 =/^\d\d$/; Start with a number with a number ending = = = "number only one number var num2 =" "; Alert (Reg2.test (num2) ); */
View Code
Iv. Recurrence of regular expressions
{n} ====> match repeats n times
{n,} = =match repeats at least n times, up to No limit
{n,m} = ="Match repeats at least n times, up to m times
* = =The match repeats at least 0 times, Maximum unlimited
+ = ="Match repeat at least 1 times, Maximum unlimited
? = = ="Match repeats at least 0 times, up to one time
/*var reg3 =/^\d{11}$/;//Mobile number ===>11 number of bits var num3 = 1234567890;alert (Reg3.test (num3));//false*///match numbers match to a minimum of 6 times, up to No limit/*var reg4 =/^\d{6,}$/;var STR4 = "123454655555555555789"; Alert (Reg4.test (STR4))*///User name minimum of 6 characters, maximum of 12 characters/*var reg5 =/^\w{6,12}$/;var STR5 = "123ab__44444a"; Alert (Reg5.test (STR5));*//*var reg6 =/^\d*$/;//matches the number, at least not, no limit var num6 = ""; Alert (Reg6.test (NUM6));*//*var Reg7 =/^\d+$/;//matches the number, at least once, without limiting var num7 = "1111111111111"; Alert (Reg7.test (NUM7));*//*var Reg8 =/^\d?$/;//matches the number, at least 0 times, at most once var num8 = "one"; Alert (Reg8.test (NUM8));*/
View Code
Five, selectors
| = = ="or
var reg1 =/com|cn/; var str1 = "com"; alert ( //COM or CN is true
View Code
six, modifier
I = = = Case insensitive
g = =Full text match (global mode)
m = = =line break match
/*var reg1 =/[a-za-z]/; Matches uppercase letters, matches the lowercase letter var str1 = "A"; Alert (Reg1.test (STR1));*//*var reg1 =/[a-z]/i; i=== "case-insensitive var str1 =" B "; alert (Reg1.test (STR1));*//*var reg1 = new RegExp ("[A-Z]", "I"), var str1 = "B"; alert (Reg1.test (STR1));*//*var reg1 =/a/g;var str1 = "abc a A"; var str = str1.replace (REG1, "#");d ocument.write (str);*//*var reg1 =/baidu/g;var str1 = "1.baidu\n2.baidu\n3.baidu"; var str = str1.replace (REG1, "#");d ocument.write (str);*//*var reg1 =/^\d/gm;var str1 = "1.baidu\n2.baidu\n3.baidu"; var str = str1.replace (REG1, "#");d ocument.write (str);*/varREG2 =/[a-z]/IgM;varSTR2 = "A A A a a a";varstr = str2.replace (REG2, "#");d ocument.write (str);
View Code
Vii. Grouping
() = ="Combine the contents."
/* var reg1 =/^ (ABC) $/;var STR1 = "abc"; Alert (Reg1.test (STR1) ); */ /* var reg2 =/^ab{3}c{3}$/; C Repeat must be 3 times var str2 = "ABBBCCC"; Alert ( reg2.test (str2) ); */ /* var reg2 =/^ (ABC) {3}$/; The ABC repetition must be 3 times var str2 = "abcabcabc"; Alert ( reg2.test (str2) ); */
View Code
Eight, character class
Regexp. (Dollar) $ ... (dollar)
Attention:
To use a character class, you must use the regular
To return results using the character class, you must have a grouping in the regular
/* var reg1 =/(a)/;var str1 = "ABC abc abc"; Reg1.test (str1); alert ( regexp.$1 );//Match to content within the first group */ / *var reg2 =/(Baidu) (Taobao) (JD)/;var str2 = "Baidu taobao JD"; Reg2.exec (str2);//Run regular alert ( Regexp.$3 ); */ var reg3 =/(taobao)/; var str3 = "I am Taobao"; var str = str3.replace (reg3, "<b style= ' color:red ' >$1</b>");d ocument.write (str);
View Code
Nine, string matching regular method
Search
If the match function subscript position
if it does not match return -1
Global not supported * * * *
Match
If the match returns an array
if the mismatch returns null
Match and exec differences
Match supports global
EXEC does not support global and can be traversed with loops
Replace
syntax: String . Method Name
/*var reg1 =/a/g;var str1 = "B A c a D"; var sh = str1.search (REG1);d ocument.write (SH);*//*var reg2 =/a/g;var str2 = "ab ac ad";//var arr = reg2.exec (STR2); Regular. exec this method = = "Does not support global//document.write (arr); var arr = Null;while (arr=reg2.exec (str2)) {document.write (arr);}*//*var reg3 =/a/g;var STR3 = "ab ac ad"; var arr = Str3.match (reg3);d ocument.write (arr);*/varREG4 =/b/G;varSTR4 = "AB BB cb";varstr = str4.replace (REG4, "*");d ocument.write (str);
View Code
javascript-Regular Expressions