1

I'm making a Chrome extension that replaces every instance of a word with a different word, but right now it only replaces the lowercase version, not uppercase. Since I'm not good with regex I thought I'd ask here. What do I need to change to make this regex case insensitive?

var replaced = $("body").html().replace(/hipster/i, 'James Montour');
$("body").html(replaced);
Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
Tom Maxwell
  • 9,273
  • 17
  • 55
  • 68

2 Answers2

3

Letter g indicate global replacement
Letter i indicate case-insensitive replacement

So you must use:

var replaced = $("body").html().replace(/hipster/ig, 'James Montour');
$("body").html(replaced);

Regards.

J.F.
  • 13,927
  • 9
  • 27
  • 65
dventura
  • 46
  • 2
1

if you want to replaces every instance of a word, you need use '/g' as well

you code could be like this:

var replaced = $("body").html().replace(/hipster/gi, 'James Montour');

$("body").html(replaced);

example:

var str="hipsterHipstER";

str.replace(/hipster/gi, 'a'); //'aa'
huan feng
  • 7,307
  • 2
  • 32
  • 56