1

I am trying to replace certain words in a CMS Article with the word as a Link.

This is for a website where I want to link certain words to different articles via the article id.

Here is my code so far:

let replace = ' (' + word + ') ';
html = html.replace(new RegExp(replace +'(?!(.(?!<a))*</a>)(?!(.(?!<img))*</img>)','i'), ' <span id="' + element.id+ '">' + replace + '</span> ');

Right now I get links on my words like 'example' or 'happy' or 'code' but I also want it to apply to (example) or (happy) or (code)

I tried to do a second replace like this:

let replace2 = ' \(' + word + '\) ';
html = html.replace(new RegExp(replace2 +'(?!(.(?!<a))*</a>)(?!(.(?!<img))*</img>)','i'), ' <span id="' + element.id+ '">' + replace2 + '</span> ');

but it doesn't work like this...

any Ideas? - Maybe I should use a totally different regex but so far a lot replaced the words in Image and normal links. This one works but I'm no expert...

AncientSwordRage
  • 7,086
  • 19
  • 90
  • 173
  • Possible duplicate of [RegEx match open tags except XHTML self-contained tags](https://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags) – AncientSwordRage Aug 06 '19 at 11:20

0 Answers0