[英]Remove non numeric characters except dash
I need to remove all non numeric characters except the dash. 我需要删除除短划线以外的所有非数字字符。 This is my attempt (based on: Regex to get all alpha numeric fields except for comma, dash and single quote ):
这是我的尝试(基于:正则表达式获取除逗号,短划线和单引号之外的所有字母数字字段 ):
var stripped = mystring.replace(/[-0-9]+/g, '');
But that doesn't work :-( 但这不起作用:-(
I'd suggest: 我建议:
var stripped = string.replace(/[^0-9\-]/g,'');
^
in the character class (within the [
and ]
) is the NOT operator, so it matches characters which are not 0-9
or the (escaped) -
character. ^
在字符类(内[
和]
)是NOT操作符,所以它匹配其不是字符0-9
或(逸出) -
字符。
As noted in the comment to this answer, by Ted Hopp , it's not necessary to escape the -
when it's the last character, but I habitually do so in order to save having to remember that proviso. 正如Ted Hopp对这个答案的评论所指出的那样,没有必要逃避
-
当它是最后一个角色时,但我习惯性地这样做是为了节省必须记住的附带条件。
References: 参考文献:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.