• javascript正则表达式——元字符


    元字符(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_p and p
    p 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_p and p
    p and p9p';
            arr = str.match(/pwp/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_p and p
    p and p9p';
            arr = str.match(/pWp/ig)//反斜线大写W查找非单词字符
            alert(arr);
        </script>
    </body>
    </html>

    效果图:

    (4)d    查找数字。

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>反斜线小写d</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_p and p
    p and p9p';
            arr = str.match(/pdp/ig)//查找数字。
            alert(arr);
        </script>
    </body>
    </html>

    (5)D     查找非数字字符。

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>反斜线大写D</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_p and p
    p and p9p';
            arr = str.match(/pDp/ig)//查找非数字。
            alert(arr);
        </script>
    </body>
    </html>

     

    (6)s    查找空白字符。

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>反斜线小写s,查找空白字符。</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_p and p
    p and p9p';
            arr = str.match(/psp/ig)//反斜线小写s,查找空白字符。
            alert(arr);
        </script>
    </body>
    </html>

     (7)S     查找非空白字符。

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>反斜线大写S,查找非空白字符。</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_p and p
    p and p9p';
            arr = str.match(/pSp/ig)//反斜线大写S,查找非空白字符。
            alert(arr);
        </script>
    </body>
    </html>

    (8)     匹配单词边界。

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>反斜线小写b,匹配单词边界。</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_p and p
    p and p9p';
            arr = str.match(/p.p/ig)//反斜线小写b,匹配单词边界。
            alert(arr);
        </script>
    </body>
    </html>

    (9)B    匹配非单词边界。

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>反斜线大写B,匹配非单词边界。</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_p and p
    p and p9p';
            arr = str.match(/Bp.p/ig)//反斜线大写B,匹配非单词边界。
            alert(arr);
        </script>
    </body>
    </html>

  • 相关阅读:
    移植tslib库出现selected device is not a touchscreen I understand的解决方法
    2017- 韦东山视频学员成果精选(三)
    2017-韦东山视频学员成果精选(二)
    2017-韦东山视频学员成果精选(一)
    字符设备驱动另一种写法—mmap方法操作LED
    使用ubuntu16.04配置linux内核和busybox出现错误的解决方法总结
    100000个嵌入式学习者遇到的PING不通问题,我们使用这一个视频就解决了,牛!
    推荐使用集串口,SSH远程登录和FTP传输三合一工具MobaXterm
    外设位宽为8、16、32时,CPU与外设之间地址线的连接方法
    Laravel 日志配置以及设置按日期记录日志
  • 原文地址:https://www.cnblogs.com/huanghuali/p/7654504.html
Copyright © 2020-2023  润新知