简体   繁体   English

自动完成建议的正则表达式?

[英]Regex for Auto complete suggestions?

I have a search bar, and this is my current regex.我有一个搜索栏,这是我当前的正则表达式。

 const regex = new RegExp(`^${value}`,'i')

My data is something like我的数据是这样的

Google Services 10239谷歌服务 10239
Adobe Services 10921 Adobe 服务 10921

They all have their own unique ID他们都有自己的唯一ID

My code works if I type "Go" then it auto suggests google which works!如果我输入“Go”,我的代码就可以工作,然后它会自动建议谷歌哪个有效!

the issue is if I type its number 10239 it doesn't work anymore.问题是,如果我输入它的编号 10239,它将不再起作用。

Now I could just reverse the order, and it should work but is there any easier way?现在我可以颠倒顺序,它应该可以工作,但有没有更简单的方法?

You can remove the ^ from your regex.您可以从正则表达式中删除^ Or if you want your autocomplete to pick up beginnings of words only, you can try replacing the ^ with a \\b like so或者,如果您希望自动完成仅获取单词的开头,则可以尝试将^替换为\\b像这样

const regex = new RegExp(`\\b${value}`, 'i')

or even甚至

 const regex = new RegExp(String.raw`\b${value}`, 'i')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM