There is an arbitrary text, once entered by the user.

How to automatically select all the hyperlinks in it?
What rules for the formation of hyperlinks should be considered?

  • It starts with the protocol: http, https, ftp.
  • Starts with www .
  • Starts with // .
  • It ends with a space or a punctuation mark, except for the opening bracket and a point.
  • Does not include the point if it is followed by a whitespace character.
  • It does not end with a closing parenthesis if it contains a pair opening.

What else should be considered?

The link never links to the same site.

Perhaps there are some common ways to highlight links?

PS: This question is in English.

  • Your question has been deleted there altogether and the redirection is answered by another similar question. - Visman
  • @Visman, not deleted, but marked as a duplicate - Grundy

2 answers 2

There are no universal rules.
You will not be able to cover all the text entered by users with one regular expression.
Everybody knows that Google uses commas in its URLs, and you want to ignore punctuation marks, except for the opening bracket and full stop.

The best thing is to work out hard rules for writing URLs, for example:

  • The link starts with (?<!\w)(?:https?|goopher|ftp|skype|etc)\/\/:
  • the link ends with a space or end of the text .+?(?:\s|$)

Users themselves adapt to these rules. If you want the link to highlight, follow some rules that can be described in the help.

You can not do well all at once. Do as you like.
But the best thing is to make the work logic as simple and understandable as possible so that the user always clearly understands where the beginning and end of the text highlighted as a link will be.

  • (?<!\w) will not work in js. - Visman
  • google.com . No space or end of text. // Comment text: http://google.ru. Не пробел и не конец текста. http://google.ru. Не пробел и не конец текста. - Qwertiy

It is not strange, the regular season turned out quite simple.

All that begins with a port office, or www. to the end of the line or whitespace, but not including all punctuation marks immediately before it:

 /(https?:\/\/|ftp:\/\/|www\.)((?![.,?!;:()]*(\s|$))[^\s]){2,}/gim