?
Solved

replace text to html hyperlinks regEx that looks for <br> and </p> tags and spaces

Posted on 2010-04-08
1
Medium Priority
?
334 Views
Last Modified: 2012-05-09
I have a script that is automatically replacing www and http:// text with hyperlinked html i.e. www.google.com becomes <a href="http://www.google.com">www.google.com</a>.

The script works fine except as long as a space terminates the inputText. How can I rewrite the regEx so that the inputText gets cut off at common punctuation i.e. period, exclamation mark, and question mark as well as <br> or </p> tags?

Thanks!
function Linkify(inputText) {
	if( /href/i.test(inputText) )
		{return inputText}
    //URLs starting with http://, https://, or ftp://
    var replacePattern1 = /(\b(https?|ftp):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gim;
    var replacedText = inputText.replace(replacePattern1, '<a href="$1">$1</a>');

    //URLs starting with www. (without // before it, or it'd re-link the ones done above)
    var replacePattern2 = /(^|[^\/])(www\.[\S]+(\b|$))/gim;
    var replacedText = replacedText.replace(replacePattern2, '$1<a href="http://$2">$2</a>');

    //Change email addresses to mailto:: links
    var replacePattern3 = /(\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,6})/gim;
    var replacedText = replacedText.replace(replacePattern3, '<a href="mailto:$1">$1</a>');

    return replacedText
}

Open in new window

0
Comment
Question by:kent3800
1 Comment
 
LVL 6

Accepted Solution

by:
karoldvl earned 2000 total points
ID: 30141729
Try replacing this line:
var replacePattern2 = /(^|[^\/])(www\.[\S]+(\b|$))/gim;


with:
var replacePattern2 = /(^|[^\/])(www\.[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gim;

and see if this is the kind of behaviour you would expect.
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

592 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question