I’m trying to replace all links in a textarea element with the result of a function for that piece of the text.
Example:
url = /(^|<|\s)(www\..+?\..+?)(\s|>|$)/g;
Text = "Text with link inside www.stackoverflow.com";
text.replace(url, convert(RESULT));
document.write(text);
function convert(link){
return " XX " + link + "XX";
}
What I need is that every link found in that string, get converted to be surrounded by XX or any other string.
The fact is that I need EACH link in the text to be sent to that function, so I can replace each of them.
I’ve been searching trough the web for a couple of hours. Tried a lot of stuff. Nothing works.
Any ideas on how to do this?
Thanks in advance!
There are a few problems.
replacefunction allows a reference to a function as its second argument. You’re calling theconvertfunction, not passing a reference.replacefunction does not modify the original string. You need to save its result.convertbefore it has been defined.Putting it all together:
Output: