简体   繁体   English

删除短划线以外的非数字字符

[英]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,'');

JS Fiddle demo . JS小提琴演示

^ 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.

相关问题 删除除一个特定字符以外的所有非数字字符 - Remove all non-numeric characters except one particular one 删除除点、逗号和“$”之外的非数字字符? - Remove non-numeric characters except points, commas and '$'? 正则表达式用于删除除科学记数法之外的所有非数字字符 - Regular expression to strip all non numeric characters except scientific notation Javascript - 替换除句点之外的所有非字母数字字符 - Javascript - Replace all non-alpha numeric characters except period 选择所有非数字字符,单点和破折号除外 - Select all non-digit characters except single dot and single dash 尝试使用jQuery从文本框中删除非数字字符 - Trying to remove non numeric characters from textbox using jQuery 从字符串开头删除非数字字符的最佳方法是什么? - what is the best way to remove non numeric characters from the beginning of a string? 如何从变量中删除所有非数字字符 - How to remove all non-numeric characters from a variable 如何过滤除与Javascript中特定模式匹配的非数字字符之外的非数字字符? - How to filter out non-numeric characters except ones matching a specific pattern in Javascript? 如何使用正则表达式匹配除加号以外的所有非数字字符? - How to match all the non numeric characters except a plus symbol using regular expression?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM