JavaScript RegExp ??
JavaScript RegExp object
RegExp: ???? ?????.
RegExp? ??????
?? ???? ??? ?? ??? ?????.
?? ???? ??? ? ??? ???? ????? ??? ??? ? ????. RegExp? ? ?????.
??? ??? ?? ??? ? ????.
? ??? ???? ? ?? ??? ???? ?? ??, ?? ??, ?? ?? ??? ? ????.
??? ? ?? ??, ??? ?? ?? ?? ??? ? ????.
Syntax
var patt=new RegExp(pattern,modifiers);
?? ? ??? ??
var patt=/pattern/modifiers;
??? ??? ??? ?????. ???? ??? ????, ???? ???? ?? ?????.
??: ???? ???? ?? ??? ???? ?? ?? ?? ????? ??(?? ????? ?)? ?????. ?? ?? ??? ?????.
var re = new RegExp("\w+");
var re = /w+/;
RegExp ???
???? ????? ???? ?? ?? ???? ???? ? ?????. ??.
i - ???? ????? ???? ?? ??? ???? ? ?????.
g - ???? ?? ??? ??? ???? ? ?????(??? ? ?? ???? ???? ?? ?? ?? ?? ??).
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> </head> <body> <script> var str = "Visit PHP.cn"; var patt1 = /PHP中文網(wǎng)/i; document.write(str.match(patt1)); </script> </body> </html>
"is"? ?? ?? ??? ?? ? ????? ???? ?? ??
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> </head> <body> <script> var str="Is this all there is?"; var patt1=/is/g; document.write(str.match(patt1)); </script> </body> </html>
test()
test() ???? ??? ?? ???? ??? ?? ???? true ?? false? ?????. .
?? ???? ????? "e" ??? ?????.
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(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>
exec()
exec() ???? ????? ??? ?? ?????. ?? ?? ?? ????. ???? ??? ??? null? ?????.
?? ???? ????? "e" ??? ?????.
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> </head> <body> <script> var patt1=new RegExp("e"); document.write(patt1.exec("The best things in life are free")); </script> </body> </html>