I have to normalize a file removing part of the text. An sample string extracted from the file is the following:
我必须规范化文件,删除部分文本。从文件中提取的示例字符串如下:
"a":{"$":"word"}, "b":{"$":"100"}, "c": {"$":"2017-02-08T16:20:36+13:00"}
The target should be:
目标应该是:
"a":"word","b":"100","c":"2017-02-08T16:20:36+13:00"
I identified a regular expression that matches my requirement:
我找到了符合我要求的正则表达式:
var regex = /{"\$":(.*)"}/g;
But when I do the replacement (found similar code in *):
但是当我进行替换时(在*中找到类似的代码):
let targetString = sourceString.replace(regex, "$1");
I have to problems, only the first match is replaced and a "}" is not handled properly.
我有问题,只有第一场比赛被替换,“}”没有得到妥善处理。
"a":"word"},"b":{"$":"100"},"c":{"$":"2017-02-08T16:20:36+13:00
Below the full code and the console log:
在完整代码和控制台日志下面:
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":"word"},"b":{"$":"100"},"c":{"$":"2017-02-08T16:20:36+13:00
Cheers, Giovanni
1 个解决方案
#1
0
Try using this:
试试这个:
const targetString = data.replace(/\s*{".*?"\s*:\s*"(.*?)"}\s*/g, `"$1"`)
\s*
is used to remove all extra spacing.
\ s *用于删除所有额外的间距。
#1
0
Try using this:
试试这个:
const targetString = data.replace(/\s*{".*?"\s*:\s*"(.*?)"}\s*/g, `"$1"`)
\s*
is used to remove all extra spacing.
\ s *用于删除所有额外的间距。