mardi 12 avril 2016

Javascript advanced replace - RegExp?

I want to automatically create hyperlinks on any page based on a rule. For example, I want to find all words ending with 'ate' and turn them into hyperlinks, including the word itself. Like:

  • 'Regulate' becomes '<a href="http://ift.tt/1VmaicK">Regulate</a>'
  • 'Congregate' becomes '<a href="http://ift.tt/23BChG4">Congregate</a>'

and so on.

At first I tried splitting all childNodes into words, comparing each word to a RegExp, then using the word to construct the new text and do the replace. That did work...but if I apply it to a page of any length it takes forever.

Is there a way to do this with a straight .Replace() RegExp?




Aucun commentaire:

Enregistrer un commentaire