Andreas Köberle
Andreas Köberle

Reputation: 110892

How to replace a char that is not lead by another char

I'm looking for a JavaScript Regular Expression that will replace i in sin(i/20*i) but not in the i in sin. Using (\W)i will always give me (i in this case.

Upvotes: 0

Views: 50

Answers (1)

Joey
Joey

Reputation: 354416

You can replace

\bi\b

which will require i to stand alone (or at least not in a string of alphanumeric characters and underscores). The \b is a zero-width assertion, as it matches a position between characters, so you don't capture more than you need.

The usual cautions apply. With only a single example it's hard to give a good solution that works for whatever data you want to throw it at. Adapt accordingly.

Upvotes: 5

Related Questions