Javascript Regex for Removing http:// and https:// from text

I found this JS script here on SO that seems like it will work great for detecting both regular links and Twitter #hashtags and @usernames:

function processTweetLinks(text) {
    text = text.replace();
    var exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/i;
    text = text.replace(exp, "<a href='$1' target='_blank'>$1</a>");
    exp = /(^|\s)#(\w+)/g;
    text = text.replace(exp, "$1<a href='http://search.twitter.com/search?q=%23$2' target='_blank'>#$2</a>");
    exp = /(^|\s)@(\w+)/g;
    text = text.replace(exp, "$1<a href='http://www.twitter.com/$2' target='_blank'>@$2</a>");
    console.log(text);
}

BUT... The first expression doesn't quite suit my needs... When it gets something like http://google.com, it outputs <a href='http://google.com' target='_blank'>http://google.com</a>. I want it to output <a href='http://google.com' target='_blank'>google.com</a> instead - basically removing http:// or https:// from inside the anchor tags. I don't know regex - what would the function need to look like in order to output that?

UPDATE: I got the function fixed with @BrunoFinelli's answer, which works great, but I don't know how to make it fix multiple links in a given string/message. Right now it only fixes one each time I call the function... If anyone can adjust the function to fix this that'd be much appreciated! Thanks! It'd also be nice if the first regex (the one in question) removed www.'s as well from inside the anchor tags? But really I just need to know how to repeat this through a Tweet that may have more than one link/mention/hashtag. Thanks!

Answers


Try this one:

    function processTweetLinks(text) {
        text = text.replace();
        var exp = /(\b(https?|ftp|file):\/\/)([-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/i;
        text = text.replace(exp, "<a href='$1$3' target='_blank'>$3</a>");
        exp = /(^|\s)#(\w+)/g;
        text = text.replace(exp, "$1<a href='http://search.twitter.com/search?q=%23$2' target='_blank'>#$2</a>");
        exp = /(^|\s)@(\w+)/g;
        text = text.replace(exp, "$1<a href='http://www.twitter.com/$2' target='_blank'>@$2</a>");
        console.log(text);
    }

Need Your Help

Remove search operator (AND/OR) in multiplesearch jqGrid

javascript asp.net-mvc-2 jqgrid jqgrid-asp.net

I need to hide the operator in the search popup, but I cannot get it to work.

OpenCL doesn't find ATI card

c++ opencl gpu-programming ati

I have a notebook with ATI Mobility Radeon HD 5650, and want to use the card for computing =) After installing AMD APP SDK v.2 (installation was OK) I tried to run code samples, provided with AMD. ...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.