zoukankan      html  css  js  c++  java
  • 正则表达式:(?=a)是什么意思?

    1.(?=a) 表示我们需要匹配某样东西的前面。

    2.(?!a) 表示我们需要不匹配某样东西。

    3.(?:a) 表示我们需要匹配某样东西本身。

    4.(?<=a) 表示我们需要匹配某样东西的后面。

    5.(?<!a) 表示我们需要不匹配某样东西,与(?!a)方向相反

    例子说明:

    1.(?=a):

     console.log("我是中国人".replace(/我是(?=中国)/, "rr"))

    打印出:rr中国人    (匹配的是中国前面的'我是')

    2.(?!a):

    console.log("我是中国人".replace(/(?!中国)/, "rr"))

    打印出:rr我是中国人  

    3.(?:a):

     console.log("我是中国人".replace(/(?:中国)/, "rr"))

    打印出:我是rr人

    4..(?<=a):

    console.log("我是中国人".replace(/(?<=中国)人/, "rr")) 

    打印出:我是中国rr

    5.(?<!a):

      console.log("我是中国人".replace(/(?<!中国)/, "rr")) 

    打印出:rr我是中国人

    一些正则实际使用

    去除字符串中的中文

    console.log("aaa我是中国人111".replace(/[^u4E00-u9FA5]/g, "")) // 去除中文,输出:'aaa111' 

    去除字符串中的英文

    console.log("aaa我是中国人111".replace(/([a-z])+/g, "")) // 去除英文,输出:'我是中国人111'

    去除字符串中的数字

    console.log("aaa我是中国人111".replace(/(d)+/g, "")) // 去除数字,输出:'aaa我是中国人' 

    数字格式化

    console.log("1234567890".replace(/B(?=(?:d{3})+(?!d))/g,",")) 
    // 输出:'1,234,567,890'

    去除ip地址

    console.log("192.168.0.1".replace(/((2[0-4]d|25[0-5]|[01]?dd?).){3}(2[0-4]d|25[0-5]|[01]?dd?)/,"rr"))
    // 输出:'rr'

     参考链接https://juejin.im/post/5ceb7d9df265da1b8811ba7f

  • 相关阅读:
    js的元素对象
    js实现在末尾添加节点
    js实现点击增加文本输入框
    js的DOM对象
    js其它
    js实现99乘法表
    js
    http的六种请求方法
    11.进制
    10.Debug
  • 原文地址:https://www.cnblogs.com/lwming/p/10943193.html
Copyright © 2011-2022 走看看