0

I am trying to figure out how to look through a string and replace any word which ends at ".com" at the end with a valid url link.

for example: "google.com has launces a .." will be replaced with

"<.a href='google.com'>google.com <./a> has launches .."

// I tried following code, but it only works for finding word which starts with "www."

data.rows[j].content = data.rows[j].content.replace(
  /(^|&lt;|\s)(www\..+?\..+?)(\s|&gt;|$)/g,'<a href="https://$2" target="_blank">$2</a>'
)
user21
  • 1,261
  • 5
  • 20
  • 41
  • Why can't you just swap out the second `.+?` for `com` and remove `www\.`? You seem to have your regex already, is there something you're stuck on? – 4castle Jun 18 '16 at 00:16
  • I'm new to regex. I tried above but doesn't work – user21 Jun 18 '16 at 00:22

3 Answers3

1

https://regex101.com/r/oF0mA9/2 should do the trick from the regex front.

engineer14
  • 607
  • 4
  • 13
1

First, I recommend learning how regex works. It's a very powerful tool that all developers should understand because it appears in many different programming languages.

Once you understand the basics, this should make more sense:

/(^|&lt;|\b)(\S+?\.com)(\b|&gt;|$)/g

Regex101 Demo - (for a breakdown of the regex, look in the top-right pane)

4castle
  • 32,613
  • 11
  • 69
  • 106
  • It works but I only want to catch non spacing character which ends in '.com' or '.edu' or '.org'. The above works for words which is like google.com – user21 Jun 20 '16 at 17:28
  • @user21 That makes sense. I've updated to fix that. Btw, to allow more top level domains than `.com`, you could use alternation like `(com|edu|org)`. – 4castle Jun 20 '16 at 17:58
  • when I use data.rows[j].content = data.rows[j].content.replace( /(^|<|\b)(\S+?\.com|edu|org)(\b|>|$)/g,' $2' ); It will only replace the string to www.google com. – user21 Jun 20 '16 at 18:03
  • it works now. Thanks! data.rows[j].content = data.rows[j].content.replace( /(^|<|\b)(\S+?\.(com|edu|org))(\b|>|$)/g,' $2' ); – user21 Jun 20 '16 at 18:08
  • Is there a way to differentiate words which has has 'https:'' in front, and only replace it with " – user21 Jun 20 '16 at 18:36
  • Certainly, just add that requirement to the pattern. The middle part of the regex would be like this: `https:\S+?\.(com|edu|org)`. If you want the `s` in `https` to be optional, make it `https?` – 4castle Jun 20 '16 at 18:50
1

You can follow this ways for ending com

(http(s?):)|([/|.|\w|\s])*\.(?:com)

OR

(?i)\.(com)$

OR

(?:([^:/?#]+):)?(?://([^/?#]*))?([^?#]*\.(?:com))(?:\?([^#]*))?(?:#(.*))?

Resource Link:

Regex to check if valid URL that ends in .jpg, .png, or .gif

Community
  • 1
  • 1
SkyWalker
  • 28,384
  • 14
  • 74
  • 132