1

I have a function that pulls urls out of a chunk of text and replaces them with links. What I would like to do is to have the look at the length of $1 and if it is over 64chars long, I would like to replace it with something like "link". I am just not sure how to approach this.

var exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig; 
return text.replace(exp,"<a href='$1' target='_blank'>$1</a>"); 

Any ideas?

1 Answer 1

3

Try -

var str = "http://www.yahooghghhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhdf.co.uk";
str = str.replace(/(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig, function($0) { 
    if ($0.length > 60 ) return "<a href='" + $0 + "' target='_blank'>Link</a>" 
    else return "<a href='" + $0 + "' target='_blank'>" + $0 + "</a>"     
})
alert(str);

Demo - http://jsfiddle.net/E7hyd/

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.