Ask Your Question
0

What is the regular expression used to substitute a substring that is outside of tags?

asked 2021-09-27 11:00:00 +0000

nofretete gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2022-07-06 04:00:00 +0000

scrum gravatar image

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.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2021-09-27 11:00:00 +0000

Seen: 14 times

Last updated: Jul 06 '22