我正在尝试将 =
替换为 :
,其中它位于大括号中且不在单引号或双引号内(将 c# 构造函数转换为 javascript}:
{name = 'John', something = "a=1", another = 'b=1'}
结果将是:
{name : 'John', something = "a=1", another = 'b=1'}
我已经尝试了很多,这是我想出的最后一件事,但与我想要的完全相反(仅更改引号中的内容)
ss = ss.replace(/({[^}]+([^'"]))(=)((?:[^'"])(?:[^}]+)})/g, '$1:$4');
这是结果:
{name = 'John', something = "a=1", another = 'b:1'}
还有这个
/({[^}]+([^'"]))(=)((?:\2)(?:[^}]+)})/g
完全不匹配
最佳答案
你可以试试这个,
var s = "{name = 'John', something = \"a=1\", another = 'b=1'}";
console.log(s.replace(/=(?=\s*(['"])((?!\1).)*\1)/g, ':'))
关于javascript - 替换大括号内且未用引号引起来的字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38871830/