简体   繁体   English

我想删除除“。”之外的所有非数字和所有标点符号。

[英]I want to remove all non-digits and all punctuations except “.”

I want to remove all non-digit symbols and punctuations except ".".我想删除除“。”之外的所有非数字符号和标点符号。 I have done similar job to remove all non-digit symbols like this:我已经完成了类似的工作来删除所有像这样的非数字符号:

 if (!/^[0-9]+$/.test(this.value)) {
     this.value = this.value.replace(/\D/, "");
}

How can I do that?我怎样才能做到这一点? Thanks for helping.感谢您的帮助。

this.value = this.value.replace(/[^\d\.]/g, "");

The \D will match any character that is not a digit. \D将匹配任何不是数字的字符。

If you also don't want to match a dot, you could use a negated character class [^\d.]+ which will match any character except a dot or a digit.如果您也不想匹配点,则可以使用否定字符 class [^\d.]+将匹配除点或数字以外的任何字符。

As you are replacing it with an empty string, you could repeat the character class 1+ times.当您用空字符串替换它时,您可以重复字符 class 1 次以上。

Use the /g global flag to replace all occurrences.使用/g全局标志替换所有匹配项。

this.value = this.value.replace(/[^\d.]+/g, "");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM