0

I just can't get my head around regex and this is driving me crazy.

I have the following string;

'This is a random string of text with a link to google.com/test and another link to facebook.com`

What I want to do, is turn google.com/test into https://google.com/test but leave the facebook.com link as plain text.

So basically, any instance of google.com (Including with prefixes) would turn into a link, but any other URL would remain as plain text.

Can someone give me a nudge in the right direction please?

Dean Elliott
  • 169
  • 5
  • 12

1 Answers1

2

Simple replace() will do:

var str = 'This is a random string of text with a link to google.com/test and another link to facebook.com';
str = str.replace('google.com/test', 'https://google.com/test');
console.log(str);
Mamun
  • 66,969
  • 9
  • 47
  • 59