Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expression in javascript to remove anything that is not: a-z 0-9 and hyphen

Thanks in advance.

I would like a regular expression that removes anything that is NOT alpha numeric and a hyphen. So allowed are A-Z 0-9 and -.

Also, how could I apply that to a string in Javascript?

Thanks again.

like image 879
jamesmhaley Avatar asked Sep 01 '25 17:09

jamesmhaley


1 Answers

var str = 'a23a-asd!@#$';
str.replace(/[^-a-z0-9]/ig,'');
like image 138
Anatoliy Avatar answered Sep 04 '25 06:09

Anatoliy