开发者

problem in not replaceing minus sign(-) with a blank using regex

开发者 https://www.devze.com 2023-03-19 01:28 出处:网络
I am using this regex expression to replace some characters with \"\" I used it as query=query.replace(/[^a-zA-Z 0-9 * ? :. + - ^ \"\" _]+/g,\'\');

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号