【问题标题】:Regex - find specific pattern not surrounded by [] [duplicate]正则表达式 - 查找不被 [] 包围的特定模式 [重复]
【发布时间】:2014-10-15 08:37:07
【问题描述】:

在给定的随机字符串中:'#id1 .class1 .1class [price="23 .23 "] .7ytr"

我想替换模式 - 点后跟数字后跟 {n} 字母数字,例如:.8acb8

但是当它被“[]”包围时忽略它 - [price="23 .23 "]

现在我只写了我需要找到的模式,而没有忽略 []:

str.replace(/\.(\d+\w+)/g, '[class="$1"]');

【问题讨论】:

    标签: javascript regex


    【解决方案1】:

    您将使用负前瞻:

    \.\d\w+(?![^[]*\])
    

    解释

    \.\d\w+         # Any dot followed by a number followed by alpha-numeric characters
    (?![^[]*\])     # Which is not inside brackets (Negative Lookahead)
    

    Live demo

    【讨论】:

    • \.\d\w+(?=[^\[\]]*(?:\[|$))
    【解决方案2】:
    \[[^]]*\]|(\.\d\w+)
    

    试试这个。抓住捕获或匹配。查看演示。

    http://regex101.com/r/qU4wM7/1

    【讨论】:

    • 我想避免 [] 中的其他内容,而您的正则表达式不要跳过它们
    • @user959210 如果你使用match,那么它将跳过[]。这就是这里使用的东西。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-17
    • 1970-01-01
    • 1970-01-01
    • 2016-11-28
    • 1970-01-01
    • 2018-08-08
    相关资源
    最近更新 更多