Reputation: 3823
Have code:
var regexp = new RegExp("[^a-zA-Z\-\s]", "g");
val = val.replace(regexp,'');
It need too leave letters (a-zA-Z), - (\-) and white spaces (\s) and remove all other symbols.
But now it remove white spaces too.
What am I doing wrong?
Upvotes: 1
Views: 76
Reputation: 5151
Here is a JavaScript Regex Generator tool i came across if anyone is interested.
Was quite useful for regex amateurs like me.
Upvotes: 1
Reputation: 382454
Your slashes need to be escaped in a string literal.
A simple solution is to use a regex literal :
var regexp = /[^a-zA-Z\-\s]/g
Upvotes: 2
Reputation: 336468
You need to double the backslashes:
var regexp = new RegExp("[^a-zA-Z\\-\\s]", "g");
or, better, use a regex literal (and simplify it):
var regexp = /[^a-z\s-]/gi;
Upvotes: 3