我有一些正则表达式我遍历整个
HTML页面寻找字符串并替换它们,但是如果字符串是单引号或双引号我不希望它匹配.
当前正则表达式:([a-zA-Z _] [a-zA-Z0-9 _] *)
我想匹配史蒂夫,约翰,希望和约翰喜欢走路(x3)
但不是“史蒂夫”,“sophie”或“john”“喜欢”“蛋糕”
我试过(^“)([a-zA-Z _] [a-zA-Z0-9 _] *)(^”)但没有匹配?
测试用例:
(steve=="john") would return steve ("test"=="test") would not return anything (boob==lol==cake) would return all three
解决方法
试试这个:
(\b(?<!['"])[a-zA-Z_][a-zA-Z_0-9]*\b(?!['"]))
反对这个字符串:
john "michael" michael 'michael elt0n_john 'elt0n_j0hn' 1 2 3 4 5 6
它将匹配nr 1 john,nr 3 Michael和nr 5 elt0n_john