Replace using text() to search, and html() as returned
Hello,
What I would like to achieve is to search through the text of elements and replace it with html using .replace and regex while preserving the html structure (including descendant elements).
I fully understand that regex can't be used on html tags, so the only way to not have the input contaminated is to use the .text() property of the element. The side affect of this is that I can't .replace() with html in the replacement string as it is shown as plain text (obviously).
This is similar to scripts that automatically add anchor tags to keywords, but all I have seen will fail if a keyword is part of a html tag e.g. Replacing
with
- "<a href="http://google.com.au">Google</a>"
so that
- <div class="Google">
- "Google"
- <div>
- Something else that needs to be preserved
- </div>
- </div>
would normally end up as
- <div class="<a href="http://google.com.au">Google</a>">
- "<a href="http://google.com.au">Google</a>"
- <div>
- Something else that needs to be preserved
- </div>
- </div>
but I would like it to just be
- <div class="Google">
- "<a href="http://google.com.au">Google</a>"
- <div>
- Something else that needs to be preserved
- </div>
- </div>
And if it were just on the text, with the html set, it would end up being
- <div class="Google">
- "<a href="http://google.com.au">Google</a>"
- </div>
Where all descendant elements are removed and the tag is escaped to plain text.
Thank you for the help.