Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex to make search suggestion bold like Google or Amazon

I'm looking for solution to make text bold, like Google and Amazon search suggesters do it.

I know how to make it the opposite way (if I type "ne"), e.g. Newton with function like this:

    highlight(value, input): string {
    return value.replace(new RegExp('(^|)(' + input + ')(|$)', 'ig'), '$1<b>$2</b>$3');
}

But how should this regex look to achieve this; for example, if I type "ne" I should get:

  1. new car
  2. Super new computer
  3. Newton
like image 529
biel2u Avatar asked Jan 27 '26 21:01

biel2u


1 Answers

If the Regex you're using here covers all of your use cases, a simple solution would be to leave it (almost) exactly as it is, and modify the CSS for the desired effect:

.search-suggestion { font-weight: bold; }
.search-suggestion > span { font-weight: normal; }

Put your search suggestion in an element that has the search-suggestion class, and modify your Regex to wrap the search text in a <span> instead of a <b> element.

like image 137
Byron Jones Avatar answered Jan 30 '26 09:01

Byron Jones



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!