Oleksandr Khavdiy Oleksandr Khavdiy -4 years ago 258
Javascript Question

val.replace(/[^a-zA-Z_-0-9]/g, '') produce SyntaxError: invalid range in character class

I need to replace all chars which are not match with range

. So I do
val.replace(/[^a-zA-Z_-0-9]/g, '')
but get error. How can I bit this?

Answer Source

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, '')
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download