?????? ???
Grammar
Definition
JavaScript?? ???? ???? ???? ? ??? ????. <%XXX%>
1? ??? ???? ?????. var reg=new RegExp( ' <%[^%>]+%>','g');
2. ???
var reg=/<%[^%>]%>/ g;
g: ??, ?? ??? ??, ?? ??? ? ?? ???? ???? ?????. i: ???? ??, ?? ???? ??
m: ?? ?, ?? ? ??(^ ? $? ??? ???? ?? ???? ??? ??? ??? ?? ??? ?? ?? ??? ??? ???? ???.)
?? ??
???? ??? ????. ??? ??? ????? ??? ??? ?? ??? ??? ?? ??(?? ??? ???? ? ??? ? ?? ????? ??? ??? ?? ?? ??)? ?? ?? ?????. [ { ^ $ | ) ? * + .?? ?? ??? ?? ?? ???? ?? ??? ?? ?? ????. ??? ?????
?? ??? ?? ??
?? ??
t ?? ? ??
r ??? ?? ??
n ? ?? ??
f ? ?? ??
cX X? ???? ?? ??(Ctrl+X)
v ?? ? ??
????? ??? ?? ??? ??? ??? ????? [] ??? ??? ??? ?????. ?:
alert(/ruby/.test("ruby"));//true
alert(/[abc]/.test("a"));//true
alert(/[abc]/.test("b"));//true
alert(/[abc]/.test("c"));//true
alert( " ?? , ???, ??? ?? , ??? ???".match(/[bcf]at/gi));//bat,Cat,fAt,bat,faT,cat
???? Xiang ???
? ??? ?? ??? ??? ?? ???? ?? ?? ?? ??? ???? ?? ??? ??? ?? ??? ? ? ??? ?????.
alert(/[^abc]/.test("a"));//false
alert(/[^abc]/.test("b"));// false
alert(/[^abc]/.test("6"));//true
alert(/[^abc]/.test("gg"));//true
Scope class
??? ??? ??? ??? ??? ????. ??? ???? ??? ?? ?? ??? ???? ?? ????? ?? ??????. ??? ???? ???? ?? ????.
?? ??
? ??? ????? ?? ??? ??? ????. ???? ???? ??? ??? ?? ??? ???? ? ????.
alert(/[a-f]/.test("b"));//true
alert(/[a-f]/.test("k"));//false
alert(/[a-z]/.test("h"));//true
alert(/[A-Z]/.test("gg"));//false
alert( /[^H-Y]/.test("G"));//true
alert(/[0-9]/.test("8"));//true
alert(/ [^7-9]/.test("6"));//true
alert(/[a-m1-5n]/.test("a"))//true
alert(/[a-m1-5n]/.test("3"))//true
alert(/[a-m1-5n]/.test(a))//true
alert(/[a-m1-5n]/.test("r"))//false
?? ??? ???
?? ?? ? ??
. [^nr] ? ?? ? ??? ??? ??? ?? ??
d [0-9] ??
D [^0-9] ??? ?? ??
s [tnx0Bfr] ?? ??
S [^ tnx0Bfr] ??? ?? ??
w [a-zA-Z_0-9] ?? ??(?? ??)
W [^a-zA-Z_0-9] ??? ?? ??
alert(/d/.test("3 "))//true
alert(/d/.test("w"))//false
alert(/D/.test("w"))//true
Alert(/w/.test("w"))//true
alert(/w/.test("SI"))//false
alert(/W/.test(" " ))//true
alert(/s/.test(" "))//true
alert(/S/.test(" "))//false
alert( / S/.test("?"))//true
alert(/./.test("美"))//true
alert(/./.test(" ")) / /true
alert(/./.test(a))//true
?? ???? ???? ??? ??? ???
1. search() ???;
? ???? ????? ??? ?? ???? ????? ???? ???? ???? ???? ? ?????.
?? ??: stringObject.search(regexp);
@param regexp ?? ??? stringObject?? ???? ?? ?????? ???? ?? RegExp ??? ? ????.
@return(?? ?) ?? ??? ??? ???? stringObject? ? ?? ?? ???? ?? ?????. ???? ?? ???? ??? -1? ?????.
??: search() ???? ?? ??? ???? ??? ??? g? ???? regexp ??? lastIndex ??? ?? ??? ?? ?????. ????? ?? ???? ??? ? ?? stringObject? ???? ? ?? ??? ?????.
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php.cn</title> </head> <body> <button onclick="myFunction()">點(diǎn)擊</button> <p id="demo"></p> <script> function myFunction() { var str = "Visit W3cSchool!"; var n = str.search(/3c/i); document.getElementById("demo").innerHTML = n; } </script> </body> </html>
2. match() ???;
? ???? ??? ??? ??? ?? ????? ?? ??? ?? ???? ???? ??? ?? ? ?????. ? ???? indexOf() ?? lastIndexOf()? ????? ??? ??? ?? ??? ?? ?????.
?? ??:
stringObject.match(searchValue) ?? stringObject.match(regexp)
@ param(????)
searchValue? ??? ?? ???? ???.
regexp: ??? ???? ?? RegExp ??;
@return(?? ?)? ???? ?? ??? ??? ?????. ????? ???? ?? ??? ?????. ???? ??? ??? null? ?????. ??? ???? ? ?? ??? ??, ? ?? ???? ???? ???? ????, index ??? ???? ???? ?????. stringObject? ?? ?? ?????. input ??? stringObject ??? ?? ??? ?????.
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php.cn</title> </head> <body> <button onclick="myFunction()">點(diǎn)擊</button> <p id="demo"></p> <script> function myFunction() { var str = "hello world"; var n = str.match("world"); document.getElementById("demo").innerHTML = n; } </script> </body> </html>
3. replacement() ???:
? ???? ???? ?? ??? ??? ? ?????. ?? ?? ?? ?? ??? ???? ?? ???? ??? ? ?????. ???;
?? ??: stringObject.replace(regexp/substr,replacement);
@param (????)
regexp/substr; ?? ??? ??? ??? ?? RegExp ??.
??: ??? ?, ??? ??? ?? ?? ???? ???? ??.
@return(?? ?) ?? ? ? ???? ?????.
??: ???? stringObject? ??() ???? ?? ? ?? ??? ???? ??? ?? ???? ? ????. ?? ?? ??? ?? ?? ??? ???? ??? g? ???? ??? ? ????. ??? ??? ? ?? ?? ???? ?????.
RegExp ?? ??. 1.test() ???:
? ???? ???? ?? ??? ????? ???? ? ?????. ?? ??: RegExpObject.test(str);
@param(????) str? ?????. string;
@return(?? ?) ??? str? RegExpObject? ???? ???? ???? ??? true? ????, ??? ??? false? ?????.
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php.cn</title> </head> <body> <button onclick="myFunction()">點(diǎn)我</button> <p id="demo">請訪問網(wǎng)站</p> <script> function myFunction() { var str = document.getElementById("demo").innerHTML; var txt = str.replace("網(wǎng)站","php.cn"); document.getElementById("demo").innerHTML = txt; } </script> </body> </html>2. exec() ???:
? ???? ?? ?? ??? ???? ? ?????. ???? ??. ?? ??: RegExpObject.exec(string)
@param(?? ??): string [??] ??? ??????.
@return(?? ?): ???? ??? ??? ??? ?????. ???? ??? ??? ?? ?? null???.
??: ??? ??? ? ?? ??? ??? Text? ?????. 2?? ??? ?????. index ??? ???? ???? ? ?? ?? ??? ?????. ? ???? ??? ?? ?? ??? ??? ()?? ??? ??? ????. ??? ?????.
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php.cn</title> </head> <body> <script> var patt1=new RegExp("e"); document.write(patt1.test("The best things in life are free")); </script> </body> </html>