我想创建一个regexp,它针对字符串中不是0-9 (数字)或- (破折号)的任何字符。下面的regexp有什么问题?
正则表达式:
regex = '/^[0-9-]/g';JS实现:
$(this).val($(this).val().replace(regex, ''));发布于 2011-07-21 23:45:06
我想你的意思是让你的^在character类中。还有,regexes are not strings
regex = /[^0-9-]/g;
$(this).val($(this).val().replace(regex, ''));发布于 2011-07-21 23:43:51
您需要将“^”放在括号内并转义“-”,因为它是一个特殊的字符。您的正则表达式应该是
regex = /[^0-9\-]/ghttps://stackoverflow.com/questions/6778802
复制相似问题