简体   繁体   English

正则表达式 - 替换引号中的引号

[英]Regex - Replace quote in quotes

The following regex pattern finds the a single double quote in the following string examples (basically the double quote after the 1).以下正则表达式模式在以下字符串示例中查找单个双引号(基本上是 1 之后的双引号)。 The problem is that the positive lookbehind is not supported in some browsers.问题是某些浏览器不支持积极的后视。 Is there an alternative regex pattern that would work?是否有替代的正则表达式模式可以工作? I need to replace this double quote with another character using js (eg with a? character).我需要使用 js 将这个双引号替换为另一个字符(例如用一个?字符)。

(?<=(\w|"))"+(?![\s]) (?<=(\w|"))"+(?![\s])

abc-1"-def321 abc-1"-def321

"abc-1"-def321" “abc-1”-def321“

"aloha" “阿罗哈”

Desired results (replace double quote with a? character):期望的结果(用?字符替换双引号):

abc-1?-def321 abc-1?-def321

"abc-1?-def321" “abc-1?-def321”

"aloha" “阿罗哈”

Thanks.谢谢。

I suggest我建议

.replace(/([\w"])"+(?=\S)/g, '$1?')

See the regex demo .请参阅正则表达式演示 Details :详情

  • ([\w"]) - Capturing group 1 : a word or " char ([\w"]) -捕获第 1 组:一个单词或"字符
  • "+ - one or more " chars "+ - 一个或多个"字符
  • (?=\S) - followed with a non-whitespace char. (?=\S) - 后跟一个非空白字符。

See the JavaScript demo:请参阅 JavaScript 演示:

 const text = `abc-1"-def321 "abc-1"-def321" "aloha"`; console.log(text.replace(/([\w"])"+(?=\S)/g, '$1?'));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM