val.replace(/ [^ a-zA-Z_-0-9] / g,'')产生SyntaxError:字符类中的无效范围 [英] val.replace(/[^a-zA-Z_-0-9]/g, '') produce SyntaxError: invalid range in character class

查看:1114
本文介绍了val.replace(/ [^ a-zA-Z_-0-9] / g,'')产生SyntaxError:字符类中的无效范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要替换所有与范围 a-zA-Z_-0-9 不匹配的字符。所以我做 val.replace(/ [^ a-zA-Z_-0-9] / g,'')但是得到错误。我怎么能咬这个?
谢谢

I need to replace all chars which are not match with range a-zA-Z_-0-9. So I do val.replace(/[^a-zA-Z_-0-9]/g, '') but get error. How can I bit this? Thanks

推荐答案

如果你想在字符类中包含减号 - ,你必须把它进入范围的末尾:

If you want to include the minus sign "-" in the character class, you have to put it into the end of range:

val.replace(/[^a-zA-Z_0-9-]/g, '')

这篇关于val.replace(/ [^ a-zA-Z_-0-9] / g,'')产生SyntaxError:字符类中的无效范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆