JavaScript replace() uppercase or lowercase

javascript, replace

Solution

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.

Problem

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

Original source

Related problems