输入:
"Hello."
"'I'm not.'"
正则表达式:
/"\b/g // opening quote
/"\B/g // closing quote
输出:
“Hello.”
”'I'm not.'”
如您所见,当正则表达式只是一个单词时,它可以正常工作。当单词被单引号括起来时,就会出现问题。不确定是什么问题。有什么想法吗?
最佳答案
我会尝试在 Something-followedBy-notSomething-followedBy-something 方法上构建正则表达式。所以它可能如下所示:
var input = "\"'I'm not.'\" - said Marry.";
var rex = /"([^"]+)"/g
alert(input.replace(rex, ''$1''))
关于JavaScript:当存在单引号时,用于替换双大引号的正则表达式失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29034062/