如何使用Javascript从下面的字符串中清除29%?

This is a long string which is 29% of the others.

我需要某种方式删除所有百分比,因此代码也必须与此字符串一起工作:
This is a long string which is 22% of the others.

最佳答案

正则表达式\d+%匹配一个或多个数字,后跟一个%。然后是一个可选的空格,这样您就不会在一行中最后有两个空格。

var s = "This is a long string which is 29% of the others.";
s = s.replace(/\d+% ?/g, "");
console.log(s);
// This is a long string which is of the others.

在表达式末尾没有可选空格的情况下,您最终得到
    // This is a long string which is  of the others.
    //-------------------------------^^

关于javascript - 字符串的干净方式百分比,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8416375/

10-11 23:33
查看更多