Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

The regular expression used to substitute a substring that is outside of tags may vary depending on the context and programming languages used. However, a common approach is to use a negative lookahead and lookbehind to ensure that the substring being replaced does not occur within any HTML or XML tags.

Here's an example of a regular expression that replaces the word "hello" with "hi" only if it's not within an HTML/XML tag:

(?<!<[^>]+)hello(?!>) 

In this regex, the (?<!<[^>]+) is a negative lookbehind that matches "hello" only if it's not preceded by any HTML/XML tag. Similarly, the (?!>) is a negative lookahead that matches "hello" only if it's not followed by any closing HTML/XML tag.

This regular expression can be used in various programming languages like Python, JavaScript, PHP, etc.