开发者

Regular expression to replace multiple occurances of a characters leaving one by a blank

开发者 https://www.devze.com 2023-03-17 15:44 出处:网络
I want to implement wild card query in solr. I want that when I search for query= ****diamond, it should search for whiltediamond etc. ie all \'*\' should be replaced by \'\' (blank) except 1 * ie. it

I want to implement wild card query in solr. I want that when I search for query= ****diamond, it should search for whiltediamond etc. ie all '*' should be replaced by '' (blank) except 1 * ie. it should r开发者_如何学运维un as *diamond, I am using

query=query.replace(/[^a-zA-Z 0-9 * ? :  .]+/g,'');

It means other than *, ?, :, . it is replacing all special characters by a ''. now i want to convert it as that it should replace all * leaving one * by ''. Than what should be regular expression.


Your regex should do the following:
Replace any number of asterisks by only one asterisk:

query=query.replace(/\*+/,'*');
0

精彩评论

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