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 t
try this:
val.replace(/[^a-zA-Z_0-9-]/g, '');
The - character is treated as a literal character if it is the last or the first (after the ^) character within the brackets.
$(this).val($(this).val().replace(/[^a-zA-Z_0-9-]/g, ''));
You have a bad range, remove the -
between _
and the number range and put it at the end or at the beginning.
I would prefer this regex:
val.replace(/[^\w-]+/gi, "");
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, '')