1. 程式人生 > >javascript正則表達式——元字符

javascript正則表達式——元字符

tac -s cnblogs java pan 擁有 asp 分享 cte

元字符(Metacharacter)是擁有特殊含義的字符:

元字符 描述

(1) . 查找單個字符,除了換行和行結束符。

例子:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>點</title>
</head>
<body>
    <script type="text/javascript">
        str
=‘index and php 2 and Php and pHp 3 and indox and 20 indax and indBx andpcp and pp and p p and p-p and p\np and p9p‘; arr = str.match(/p.p/ig)//查找單個字符,除了換行和行結束符。 alert(arr); </script> </body> </html>

效果如圖:

技術分享

(2)\w 查找單詞字符。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>反斜線小寫w查找單詞字符</title>
</head>
<body>
    <script type="text/javascript">
        str
=‘index and php 2 and Php and pHp 3 and indox and 20 indax and indBx andpcp and pp and p p and p-p and p\np and p9p‘; arr = str.match(/p\wp/ig)//反斜線小寫w查找單詞字符 alert(arr); </script> </body> </html>

效果圖:

技術分享

(3) \W 查找非單詞字符。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>反斜線大寫W查找非單詞字符</title>
</head>
<body>
    <script type="text/javascript">
        str
=‘index and php 2 and Php and pHp 3 and indox and 20 indax and indBx andpcp and pp and p p and p-p and p\np and p9p‘; arr = str.match(/p\Wp/ig)//反斜線大寫W查找非單詞字符 alert(arr); </script> </body> </html>

效果圖:

技術分享

javascript正則表達式——元字符