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
  • 166,785
  • 24
  • 204
  • 249
Tom Maxwell
  • 8,773
  • 16
  • 53
  • 67

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.
  • 11,073
  • 8
  • 23
  • 48
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
  • 6,189
  • 1
  • 26
  • 49