2

Convert all occurrence of www in string with http://www I have tried

Regular expression: /^www(?!http\:\/\/|https\:\/\/)./g; but it's working only in case when any character or digit don't precede www. For example: Find me at www.test.com in this case it's failing but it's working for www.google.com find me on this. I don't know how to put condition in regular exp for this.

function mailify(text)
{
  var regEx = /^www(?!http\:\/\/|https\:\/\/)/g;
  return text.replace(regEx, "http://www");
}

console.log(mailify("www.ni@gmail.com"));
Nishant Kumar
  • 5,995
  • 19
  • 69
  • 95

7 Answers7

1

You can do this:

return text.replace("www", "http://www");
Spencer Wieczorek
  • 21,229
  • 7
  • 44
  • 54
  • Hey ur reg ex is not working check with this : console.log(mailify("fine ksajd kajsl d www.ni@gmail.com")); it is replacing http://www.ni@gmail.com while it should be fine ksajd kajsl d http://www.ni@gmail.com – Nishant Kumar Jan 30 '14 at 09:36
  • Misread, I thought it was extracting. But if it's just text-to-text replacement you can use two strings. – Spencer Wieczorek Jan 30 '14 at 09:50
  • Thanks! It was more of doodling then a project. Just wondering, does my new answer solve the problem? – Spencer Wieczorek Jan 30 '14 at 19:41
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/46482/discussion-between-nishant-and-spencer-wieczorek) – Nishant Kumar Jan 31 '14 at 05:47
0

Nishant this is helpful:

  function mailify(text)
    {
      var regEx = /www/g;
      return text.replace(regEx, "http://www");
    }

    console.log(mailify("www.google.com find me on this"));
Poonam Gokani
  • 375
  • 1
  • 4
  • 13
0

It's because your regular expression begins with a ^ which means your expression has to start with www. Try that:

/^.*www(?!http\:\/\/|https\:\/\/)./g;

Source : Regular Expressions- Match Anything.

Community
  • 1
  • 1
TSE
  • 356
  • 1
  • 8
0

Place the lookahead before the www:

var regEx = /^(?!https?:\/\/)www/;
Toto
  • 89,455
  • 62
  • 89
  • 125
0

You can use the below regex :

/([^/]|^)(www)/g

Code :

var reg=/([^/]|^)(www)/g
var line="something www.some.com , http://some1.com"
console.log(line.replace(reg,"$1http://$2"))

OUTPUT:

 "something http://www.some.com , http://some1.com"

Demo

Explanation :

enter image description here

Community
  • 1
  • 1
Sujith PS
  • 4,776
  • 3
  • 34
  • 61
  • your reg ex is failing for console.log(mailify("fine ksajd kajsl d http://www.ni@gmail.com")); Here output is comming: fine ksajd kajsl d http://http://www.ni@gmail.com – Nishant Kumar Jan 30 '14 at 09:43
  • Your code is failing : "niasodjhttp://www niosjado sdjfklsjdl f www.test.com www.machine,com" – Nishant Kumar Jan 30 '14 at 09:57
0

You can try this:

    function mailify(text)
    {
      var regEx = /\b(https?:\/\/)?www\./g;
      return text.replace(regEx, 'http://www.');
    }

    console.log(mailify('www.yourlink.com notalinkwww.yourlink.com http://www.yourlink.comm'));

You also need to check there is a dot after 'www' to make sure that it marks the start of a link.

If you just want the links to be CLICKABLE, you have to do this:

function mailify(text)
{
  var regEx = /\b((https?:\/\/)?www\.\S*)/g;
  return text.replace(regEx, '<a href="$1">$1</a>');
}
Andy
  • 3,631
  • 2
  • 23
  • 32
0

Replace any text having http:// or https or ftpor file or only www or http://www or https://wwwand followed by any character digit and special character and even back spaces into click able link.

Reg Ex: /(\b((https?|ftp|file):\/\/|(www))[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|]*)/ig

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

I have tested by below string:

  1. Find me on to www.google.com
  2. www
  3. Find me on to www.http://www.com
  4. Follow me on : http://www.nishantwork.wordpress.com
  5. http://www.nishantwork.wordpress.com
  6. Follow me on : http://www.nishantwork.wordpress.com
  7. https://stackoverflow.com/users/430803/nishant

Note: If you don't want to pass www as valid one just use below reg ex: /(\b((https?|ftp|file):\/\/|(www))[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig

Reference: How to replace plain URLs with links?

Please add as a comment if reg ex is failing and please let me know if I am wrong.

Community
  • 1
  • 1
Nishant Kumar
  • 5,995
  • 19
  • 69
  • 95