?????? ???
?? ???(??: ?? ???, ?? ???? regex, regexp ?? RE? ???)? ?? ???? ???? ?? ?? ??? ??? ??? ??? ?? ??? ???? ??????.
??? ?? ? ??? ??? ??? ? ?? ?? ?????.
?? ????? ??????
?? ???? ??? ??? ??? ?? ?????.
????? ???? ??? ? ?? ??? ???? ????? ??? ??? ? ????.
?? ???? ??? ??? ?? ?? ? ??? ??? ?? ????.
???? ?? ??? ?? ? ??? ??? ??? ??? ? ????.
??
/pattern/modifiers;
?:
var patt = /phpl/i
?? ??:
/php/i? ?? ??????.
php? ?????(??? ???).
i? ??????(??? ????? ???? ????).
??? ??? ??
JavaScript?? ???? ????? ? ?? ??? ???, search() ? ???()? ?????.
search() ???? ????? ??? ?? ???? ????? ?? ???? ???? ?? ???? ???? ?? ???? ?? ??? ???? ? ?????.
replace() ???? ???? ?? ??? ?? ??? ???? ???? ???? ?? ???? ??? ? ?????.
search() ???? ???? ?????
?
????? ???? ?? "php.cn" ???? ????? ???? ?????.
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>PHP中文網(wǎng)(php.cn)</title> </head> <body> <p>搜索字符串 "php.cn", 并顯示匹配的起始位置:</p> <button onclick="myFunction()">點(diǎn)我</button> <p id="demo"></p> <script> function myFunction() { var str = "Visit php.cn!"; var n = str.search(/php.cn/i); document.getElementById("demo").innerHTML = n; } </script> </body> </html>
????? ???? ??? ???.
search() ???? ???? ?????.
?? ???? ???? ????? ??? ? ????. . ??? ????? ?? ????? ?????:
Example
????? "php.cn"? ?? ???? ?????:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>PHP中文網(wǎng)(php.cn)</title> </head> <body> <p>搜索字符串 "php.cn", 并顯示匹配的起始位置:</p> <button onclick="myFunction()">點(diǎn)我</button> <p id="demo"></p> <script> function myFunction() { var str = "Visit php.cn!"; var n = str.search("php.cn"); document.getElementById("demo").innerHTML = n; } </script> </body> </html>
????? ???? ??? ???
replace() ??? ???? ????? Expression
Example
???? ????? ???? ?? ???? Microsoft? php.cn?? ????.
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>PHP中文網(wǎng)(php.cn)</title> </head> <body> <p>替換 "microsoft" 為 "php.cn" :</p> <button onclick="myFunction()">點(diǎn)我</button> <p id="demo">請(qǐng)?jiān)L問 Microsoft!</p> <script> function myFunction() { var str = document.getElementById("demo").innerHTML; var txt = str.replace(/microsoft/i,"php.cn"); document.getElementById("demo").innerHTML = txt; } </script> </body> </html>
????? ???? ??? ???
replace() ??? ???
replace() ???? ???? ???? ????? ?????.
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>PHP中文網(wǎng)(php.cn)</title> </head> <body> <p>替換 "Microsoft" 為 "php.cn" :</p> <button onclick="myFunction()">點(diǎn)我</button> <p id="demo">請(qǐng)?jiān)L問 Microsoft!</p> <script> function myFunction() { var str = document.getElementById("demo").innerHTML; var txt = str.replace("Microsoft","php.cn"); document.getElementById("demo").innerHTML = txt; } </script> </body> </html>
????? ???? ??? ???.
?:
?? ??? ????? ? ????? ?? ??? ? ????. ??? ????).
???? ?? ??? ?? ???? ????(?: ?/???? ???? ??).
?? ??? ???
???? ?? ???? ????? ??? ? ????.
??? | ?? |
---|---|
i | ???? ??- ??? ??. |
g | ?? ??? ?????(? ?? ?? ??? ?? ? ???? ?? ?? ?? ?? ??). |
m | ?? ? ??? ?????. |
?? ??? ??
???? ??? ??? ?? ? ?????.
Expression | Description |
---|---|
[abc] | ??? ?? ??? ?? ??? ????. |
[0-9] | 0?? 9??? ??? ????. |
(x|y) | |? ??? ??? ????. |
?? ??? ??? ??? ?? ?????.
?? ?? | Description |
---|---|
d | ??? ????. |
s | ?? ??? ????. |
b | ?? ??? ??????. |
uxxxx | 16?? xxxx? ??? ???? ??? ????. |
???:
Quantifier | ?? |
---|---|
n+ | ? ?? ??? n? ???? ?? ???? ?????. |
n* | ? 0? ??? n? ???? ?? ???? ?????. |
n? | ? 0? ?? 1?? n? ???? ?? ???? ?????. |
RegExp ?? ??
JavaScript?? RegExp ??? ?? ??? ??? ???? ?? ??? ?????.
test() ??
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>
????? ???? ??? ???
?? ? ?? ??? ??? ??? ??? ??? ????.
/e/ .test("???? ?? ?? ?? ?????!")
exec() ??
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>
????? ???? ??? ???