text = abc "def"g'i' jklm'no' p"qrs uv w"
应该变成 abc !string! G弦! jklm!字符串! p !string!
到目前为止我有什么:
if(/(?:'[^']+'|"[^"]+")/.test(text){
text = text.replace(/(?:'[^']+'|"[^"]+")/, "!string!");
}
(杰瑞的回答)
它只替换第一次出现的用 '' 或 ""括起来的文本。这实际上与我之前的问题有关,但我认为这更复杂:How to search for the second occurence of ' or " in regex?
最佳答案
g修饰符执行全局匹配
text = text.replace(/(?:'[^']+'|"[^"]+")/g, "!string!");
关于javascript - 替换所有包含在 "or ' 中的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18354263/