Replace something only if it is not surrounded by something

  bbcode, javascript, regex

Let text be

[b]Hello[/b]
[code]Hello[/code]
Hello

This is BBcode.


Essentially, any text surrounded by [b] and [/b] make the text inside bold, and the same for the second line, except it properly formats the code. The last line is regular text.

I want to write a JavaScript program that replaces "Hello" with "Hi" only if it is not surrounded by [code][/code] tags. Thus, I want the result to look like this:

[b]Hi[/b]
[code]Hello[/code]
Hi

Source: Ask Javascript Questions

LEAVE A COMMENT