Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript replace() uppercase or lowercase

I want to use this function but preserving the uppercase or lowercase so for example having something like

var value = 'Replace a string';

var search = 'replace';

value.replace(search, "<span style='font-weight: bold'>"+search+'<span>');

And the output of value be:

Replace a string

like image 709
Jerome Ansia Avatar asked Dec 07 '22 14:12

Jerome Ansia


1 Answers

Since you're leaving the word itself as-is, you can use a simple regex:

var value = 'Replace a string';
var search = /replace/i;
value = value.replace(search, "<span style='font-weight: bold'>$&</span>");

The $& indicates the matched pattern.

like image 55
Niet the Dark Absol Avatar answered Dec 27 '22 13:12

Niet the Dark Absol