在给定的随机字符串中:'#id1 .class1 .1class [price="23 .23 "] .7ytr"
我想替换模式 - 点后跟数字后跟{n}字母数字,如:.8acb8
但是当它被" []"包围时,请忽略它。喜欢 - [price="23 .23 "]
。
现在我只写了我需要找到的模式,而不是忽略[]:
str.replace(/\.(\d+\w+)/g, '[class="$1"]');
答案 0 :(得分: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 强>
答案 1 :(得分:0)