0

How can I get the url string from a generated text. For example I have generated a text

text = "This is my sample text try http:\/\/sample.com\/23411"; convertedString = "This is my sample text try http://sample.com/23411"; 

I want to do this

var disp = ""; disp += "This is my sample text try"; disp += "<a href = 'http://sample.com/23411'>http://sample.com/23411</a>"; 

result

This is my sample text try http://sample.com/23411

1 Answer 1

2

Easiest way would be to use a regex:

disp = text.replace(/(http:\/\/[^ ]+)/, "<a href='$1'>$1</a>"); 

See it here in action: http://jsfiddle.net/49YFF/

Sign up to request clarification or add additional context in comments.

3 Comments

Great! but how can I get the first text? The "This is my sample text try"? Sorry for just a simple question for you but I'm just new to Javascript.
@RobinCarloCatacutan - Did you click on that link I posted?
I didn't see that before I refresh. Tnx got to try.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.