Regexp JavaScript for binding URLs and emails in bindings

I searched high and low, but cannot find a definitive answer to this question. As often happens with regular expressions. So I thought I would ask here.

I am trying to compile a regular expression that I can use in JavaScript to replace all instances of URLs and email addresses (not necessarily be so strict) with anchor tags pointing to them.

Obviously, this something is usually done very simply on the server side, but in this case you need to work with simple text, so an elegant JavaScript solution for executing notes at runtime would be ideal.

The Onl problem, as I said earlier, I have a huge regex as a gaping hole in my skill set :(

I know that one of you has an answer at your fingertips, though :)

+3
source share
5 answers

Ok, blindly using regular expressions from http://www.osix.net/modules/article/?id=586

var emailRegex = 
   new RegExp(
   '([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}' + 
   '\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.' + 
   ')+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)',
   "gi");

var urlRegex = 
   new RegExp(
   '((https?://)' + 
   '?(([0-9a-z_!~*\'().&=+$%-]+: )?[0-9a-z_!~*\'().&=+$%-]+@)?' + //user@ 
   '(([0-9]{1,3}\.){3}[0-9]{1,3}' + // IP- 199.194.52.184 
   '|' + // allows either IP or domain 
   '([0-9a-z_!~*\'()-]+\.)*' + // tertiary domain(s)- www. 
   '([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]\.' + // second level domain 
   '[a-z]{2,6})' + // first level domain- .com or .museum 
   '(:[0-9]{1,4})?' + // port number- :80 
   '((/?)|' + // a slash isn't required if there is no file name 
   '(/[0-9a-z_!~*\'().;?:@&=+$,%#-]+)+/?))',
   "gi");

then

text.replace(emailRegex, "<a href='mailto::$1'>$1</a>");

and

text.replace(urlRegex, "<a href='$1'>$1</a>");

can work

+4
source

Unserved solution, but it will point you in the right direction.

I use Regex Coach to create and test my regular expressions. You can find many examples of regular expressions for URLs and email addresses on the Internet.

+1
source

URL-...

http://www.codinghorror.com/blog/archives/001181.html

, .tld , , , ...

[^\s] + @\ [\ -.]. * [A-Za-Z] +

+1

, ("this" " HTML " ) . , :

  • , , innerHTML
  • , (/(<a\b.+?</a>/ig)
  • , " " - " " -, orderd
  • " " (, "<a "), URL-
  • , <a>
  • join()
  • innerHTML

, , URL-. , , 4.).

0

Just add some info on regular expressions email. Most of them seem to ignore that domain names may contain the characters "åäö". Therefore, if you care about this, make sure that the solution you use has åäöÅÄÖ in the domain part of the regular expression.

0
source

Source: https://habr.com/ru/post/1703693/


All Articles