Vicky
Vicky

Reputation: 1235

Parse textarea to return URL from content

I have textarea containing one or more URLs, along with other text content. I want a JavaScript function to return the text content along with the correctly formatted URL with anchor tags.

Example contents:

abcd http://stackoverflow.com xyz

I would like the function to return:

  abcd <a href="http://stackoverflow.com">http://stackoverflow.com</a> xyz

What steps are needed to implement this request?

would be grateful for help.

Upvotes: 0

Views: 252

Answers (1)

Esailija
Esailija

Reputation: 140236

"abcd http://stackoverflow.com xyz".replace( /https?:\/\/[^ ]+/g, '<a href="$&">$&</a>')
//"abcd <a href="http://stackoverflow.com">http://stackoverflow.com</a> xyz"

Upvotes: 4

Related Questions