〜{}

时间:2018-12-06 10:28:40

标签: javascript regex string

我有这个输入字符串

~{RegExr1234124124.} was ~{created by gskinner.com}, and is ~{proudly hosted} by Media Temple.

并想要此输出:

was, and is by Media Template

我使用了/~{.*}+/g正则表达式模式,但这是错误的

1 个答案:

答案 0 :(得分:2)

.*原因匹配字符串中{之后的所有字符。

使用/~{[^}]+}/匹配~{}中的每个字符串,并使用.replace()

删除它们
var newStr = str.replace(/~{[^}]+}/g, '');

var str = "~{RegExr1234124124.} was ~{created by gskinner.com}, and is ~{proudly hosted} by Media Temple.";
console.log(str.replace(/~{[^}]+}/g, '').trim());