views:

27

answers:

1

I need a javascript to replace i into I. This should apply to cases such as

  • i'm good.
  • So am i.
  • He though i love him.

The standard ThisContent = ThisContent.replace("i", "I"); doesn't work because it replaces every i. I also thought of ThisContent = ThisContent.replace(" i ", " I "); but it doesn't work for the first and second case.

Any idea?

+2  A: 

Use regex:

ThisContent = ThisContent.replace(/\bi\b/g, "I");

Here \b indicates a "word boundary", so only word-like i's will be replaced.

KennyTM
Thanks. How about a word, say bing. I want to replace it with Bing. The above function doesn't seem to work.
Eric Sim
`.replace(/\bi\b/g, "I").replace(/\bbing\b/g, "Bing")`
KennyTM
Thanks a million, Kenny!
Eric Sim