js和java使用string的replace时需要注意的

通常是写java和js代码时造成的混淆,js使用replace去替换,如:常用去空方式,

String.prototype.trim = function() {
            return this.replace(/(^\s*)|(\s*$)/g, "");
        }

所以写java时有可能犯错,比如 String str = "as2131qwq", 如果我们想去掉str中的数字,很容易写成:

str.replace("[0-9]","");

翻看jdk, replace只做简单字符替换,因此该使用replaceAll 。

js和java使用string的replace时需要注意的_第1张图片

犯过一次错,在此记录一下。

你可能感兴趣的:(js和java使用string的replace时需要注意的)