problem in not replaceing minus sign(-) with a blank using regex
I am using this regex expression to replace some characters with ""
I used it as
query=query.replace(/[^a-zA-Z 0-9 * ? : . + - ^ "" _]+/g,'');
But when my query 开发者_如何学Cis as +White+Diamond
, i get result +White+Diamond
, but when query is -White+diamond
i am getting White+diamond
, it means -
is replaced by ""
that i don't want.
Please tell me what is the problem.
In regex, -
means "from ... to ...", escape your -
with a backslash: \-
.
What SteeveDroz said:
query=query.replace(/[^a-zA-Z0-9*?:.+\-^"_ ]+/g,'');
I'm assuming you want to exclude spaces as well. If not, remove the final space from the character class.
精彩评论