正则表达式,用于检测纯格式和降价的url [英] Regex for detecting url in plain form and in markdown

查看:70
本文介绍了正则表达式,用于检测纯格式和降价的url的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在文本区域中捕获用户输入,该文本区域可能是三种格式中的任何一种的url(和类似的电子邮件)-

I am trying to capture user input in a textarea that might be a url (and similarly email) in any of the three formats -

  1. 只是普通网址.
  2. 带有标题的降价 [text](URL"title")
  3. 无标题的降价 [text](url)

现在,对于每种可以单独使用的三种格式,我都有一个正则表达式(javascript).但是,如果我要全部执行3个,第一个将阻止第二个和第三个被激活.在我的代码中,在空间"上,正则表达式检测被触发.因此,如果我有第一个正则表达式,则永远不会触发带有降价标题的正则表达式.

Now, I have a regex (javascript) for each of the three individual formats that work by themselves. But if I want to do all 3, the first one prevents the second and third one from activating. In my code, on 'space', the regex detection is triggered. Therefore, if I have the first regex, then the one with markdown title is never triggered.

我想知道是否可以为第一个正则表达式专门排除第二和第三种格式?或者,甚至更好的是,如果有一个用于捕获所有三个匹配的正则表达式?

I am wondering if it is possible to have a regex for the 1st one that specifically excludes the format of the 2nd and the 3rd? Or, even better, if there is a single regex for capturing that matches all 3?

而且,由于我对Regex的了解不强,如果有人也可以解释其解决方案Regex,我很想知道,以便我可以尝试对电子邮件进行同样的检测.

Also, since I am not that good at Regex, I'd love if someone could also explain their solution Regex, so that I could try to do the same for email detection.

谢谢!

推荐答案

首先,第二个正则表达式已经工作对于第三种格式,因此我们只需要加入第一种和第二种格式即可.

Firstly, the second regex already works for the third format, so we only need to join the first and second ones.

执行此操作的简单方法是使用 | ("OR")字符,如下所示:

The simple way to do this is to use the | ("OR") character, like this:

()|()

演示

此问题是,它会使捕获组混乱.如果正则表达式捕获了第一个模式,则该URL将与另一个捕获组(第二个组)捕获的捕获组不同(在我的演示中为第4个).

The problem with this is that it mess the capturing groups. If the regex catches the first pattern, the url will be in a different capturing group (4th on my demo) than if it was captured by the second one (2nd group).

在普通网址格式的开头添加(?:^ | [^ \(\/])会强制正则表达式匹配任何不是开头括号的字符,从而排除了markdown这种情况下,必须使用捕获组来提取网址,因为该字符将包含在匹配项中.

Adding (?:^|[^\(\/]) to the beginning of the plain URL pattern will force the regex to match any character that's not a opening parenthesis, thus excluding the markdown case. The url must be extracted using a capturing group, since this character will be included in the match.

演示

这篇关于正则表达式,用于检测纯格式和降价的url的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆