[英]How to convert a capturing group to a non-capturing group in this regex?
我正在尝试使用正则表达式将文本中的所有 url 替换为超链接。 网址必须以http://
或https://
开头。 而且它们必须包含一些 TLD,例如.com
、 .org
或.co.uk
等。
下面是我在PHP
中的regex
模式:
$pattern = "/(http)+(s)?:\/\/(\S)+(\.){1}/i";
所以如果你使用下面的代码:
$str = "this http://dd is a String http://www.example.com and this a String https://anotherexample.co.uk";
echo preg_replace($pattern, "<a href='$0' target='_blank'>$0</a>", $str);
它给了我以下输出:
您可以看到 TLD 部分未包含在超链接中。 那么如何将捕获组(\.){1}
转换为非捕获组以覆盖 TLD?
你可以试试这个:
$pattern = "/https?:\/\/(\S)+(\.\w{1,4})+/i"
echo preg_replace($pattern, "<a href='$0' target='_blank'>$0</a>", $str);
在模式中:
https?
: http 或 https
(\.\w{1,4})+
:像.co .com或类似.co.uk的 TLD,这里每个 TLD 的最大长度为 4,但您可以更改它。
使用以下模式:
https?:\/\/[az]+\.[az]+[.az]* /i
?
[az]+
捕获 'https' 后的第一组字母[.az]*
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.