正则表达式替换多次出现的字符,留下一个空白

发布于 2024-11-18 20:13:50 字数 448 浏览 4 评论 0原文

我想在solr中实现通配符查询。我希望当我搜索 query= ****diamond 时,它应该搜索whiltediamond 等。即所有 '*' 都应该替换为 ''(空白)除了 1 * 即。它应该作为 *diamond 运行,我正在使用

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

它意味着除了 *?:. 它将所有特殊字符替换为 ''。现在我想对其进行转换,因为它应该替换所有 *,留下一个 *''。 比什么应该是正则表达式。

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 run 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笑梦风尘 2024-11-25 20:13:50

您的正则表达式应执行以下操作:
仅用一个星号替换任意数量的星号:

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

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

query=query.replace(/\*+/,'*');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文