开发者

javascript replace i into I

开发者 https://www.devze.com 2022-12-29 01:49 出处:网络
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.

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 = ThisC开发者_JS百科ontent.replace(" i ", " I "); but it doesn't work for the first and second case.

Any idea?


Use regex:

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

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

0

精彩评论

暂无评论...
验证码 换一张
取 消