简体   繁体   中英

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
Adobe Services 10921

They all have their own unique ID

My code works if I type "Go" then it auto suggests google which works!

the issue is if I type its number 10239 it doesn't work anymore.

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

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

or even

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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