Ovo*_*llo 0 javascript regex string replace
我正在寻找一种方法从另一个子串中删除大括号内的任何子串.
例如:
"The cat is {not} mine",我想要一个返回的函数"The cat is mine"; "the glass was broken {by the dog}",我想要一个返回的功能"The glass was broken "我已经读过可以在replace函数中使用正则表达式,但我不知道如何使用它.
这有效:
var s="The {yellow} cat is {not} mine";
s.replace(/\s?\{[^}]+\}/g, ''); // "The cat is mine"
Run Code Online (Sandbox Code Playgroud)
正则表达式允许可选的前导空格s?,然后查找开始文字括号\{,而不是搜索组[]+中不是右括号的多个字符^}- 直到文字结束卷曲}.
所有这一切都使用g全局标志进行多次替换.
一切都被取代了"".
<[this stuff]>分隔符<[]>:
var s = "The <[yellow]> cat is <[not]> mine";
s = s.replace(/\s*<\[[^\]\>]*\]>/g, "");
console.log( s ); // "The cat is mine"
Run Code Online (Sandbox Code Playgroud)