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?
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;
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
Here is a JavaScript Regex Generator tool i came across if anyone is interested.
Was quite useful for regex amateurs like me.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.