Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/478.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
正则表达式匹配替换多次(javascript)_Javascript_Regex - Fatal编程技术网

正则表达式匹配替换多次(javascript)

正则表达式匹配替换多次(javascript),javascript,regex,Javascript,Regex,我必须规范化删除部分文本的文件。 从文件中提取的示例字符串如下所示: "a":{"$":"word"}, "b":{"$":"100"}, "c": {"$":"2017-02-08T16:20:36+13:00"} 目标应该是: "a":"word","b":"100","c":"2017-02-08T16:20:36+13:00" 我确定了一个符合我要求的正则表达式: var regex = /{"\$":(.*)"}/g; 但当我进行替换时(在stackoverflow中发现类似的代

我必须规范化删除部分文本的文件。 从文件中提取的示例字符串如下所示:

"a":{"$":"word"}, "b":{"$":"100"}, "c": {"$":"2017-02-08T16:20:36+13:00"}
目标应该是:

"a":"word","b":"100","c":"2017-02-08T16:20:36+13:00"
我确定了一个符合我要求的正则表达式:

var regex = /{"\$":(.*)"}/g;
但当我进行替换时(在stackoverflow中发现类似的代码):

我必须解决一些问题,只有第一个匹配项被替换,并且“}”没有正确处理

"a":"word"},"b":{"$":"100"},"c":{"$":"2017-02-08T16:20:36+13:00
下面是完整代码和控制台日志:

let jsonC1 = `"a":{"$":"word"}, "b":{"$":"100"}, "c":{"$":"2017-02-08T16:20:36+13:00"}`;
//matches (regex) all white spaces and replace them with no space
let jsonN = jsonC1.replace(/\s/g,'');
console.log(`jsonN: ${jsonN}`);
var regex = /{"\$":(.*)"}/g;
let jsonS = jsonN.replace(regex, "$1");
console.log(`jsonS: ${jsonS}`);
jsonN:“a:{“$”:“word”},b:{“$”:“100”},c:{“$”:“2017-02-08T16:20:36+13:00”}

jsonS:“a:”单词“,“b:”{“$”:“100”},“c:{“$”:“2017-02-08T16:20:36+13:00

干杯,乔瓦尼

试试这个:

const targetString = data.replace(/\s*{".*?"\s*:\s*"(.*?)"}\s*/g, `"$1"`)

\s*
用于删除所有多余的空格。

*?
替换
*
…替换(/\{“\$”:\}/g,”)
应该做这项工作。
/\s*{“\$”:([^“]*”)\/g
或者
/\s*{“\\$”:(“*?”)\”)/g
应该做这项工作。谢谢大家。谢谢大家。福克斯:你的代码结果:“一个单词”b:“100,”c:“2017-02-08T16:20:36+13:00-LukStorm:第二个成功,”括号中是魔术,不知道为什么?是必需的,尽管-@RobG:您的代码工作得很好,但是…替换“”让我感到困惑!不明白它是如何工作的。
const targetString = data.replace(/\s*{".*?"\s*:\s*"(.*?)"}\s*/g, `"$1"`)