使 url regex 全局化 [英] Making a url regex global

查看:21
本文介绍了使 url regex 全局化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找一个正则表达式来替换字符串中的纯文本网址(该字符串可以包含 1 个以上的网址),通过:

I've been searching for a regex to replace plain text url's in a string (the string can contain more than 1 url), by:

 <a href="url">url</a>

我发现了这个:http://mathiasbynens.be/demo/url-regex

我想使用 diegoperini 的正则表达式(根据测试是最好的):

I would like to use the diegoperini's regex (which according to the tests is the best):

_^(?:(?:https?|ftp)://)(?:\S+(?::\S*)?@)?(?:(?!10(?:\.\d{1,3}){3})(?!127(?:\.\d{1,3}){3})(?!169\.254(?:\.\d{1,3}){2})(?!192\.168(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\x{00a1}-\x{ffff}0-9]+-?)*[a-z\x{00a1}-\x{ffff}0-9]+)(?:\.(?:[a-z\x{00a1}-\x{ffff}0-9]+-?)*[a-z\x{00a1}-\x{ffff}0-9]+)*(?:\.(?:[a-z\x{00a1}-\x{ffff}]{2,})))(?::\d{2,5})?(?:/[^\s]*)?$_iuS

但我想让它全局替换字符串中的所有 url.当我使用它时:

But I want o make it global to replace all the url's in a string. When I use this:

/_(?:(?:https?|ftp)://)(?:\S+(?::\S*)?@)?(?:(?!10(?:\.\d{1,3}){3})(?!127(?:\.\d{1,3}){3})(?!169\.254(?:\.\d{1,3}){2})(?!192\.168(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\x{00a1}-\x{ffff}0-9]+-?)*[a-z\x{00a1}-\x{ffff}0-9]+)(?:\.(?:[a-z\x{00a1}-\x{ffff}0-9]+-?)*[a-z\x{00a1}-\x{ffff}0-9]+)*(?:\.(?:[a-z\x{00a1}-\x{ffff}]{2,})))(?::\d{2,5})?(?:/[^\s]*)?_iuS/g

它不起作用,我如何使这个正则表达式全局化,开头的下划线和结尾的_iuS"是什么意思?

It does not work, how do I make this regex global and what does the underscore at the beginning and the "_iuS", at the end, means?

我想将它与 php 一起使用,所以我正在使用:

I would like to use it with php so I am using:

preg_replace($regex, '<a href="$0">$0</a>', $examplestring);

推荐答案

下划线是正则表达式分隔符,i、u 和 S 是模式修饰符:

The underscores are the regex delimiters, the i, u and S are pattern modifiers :

我(PCRE_CASELESS)

i (PCRE_CASELESS)

If this modifier is set, letters in the pattern match both upper and lower 
case letters.

U (PCRE_UNGREEDY)

U (PCRE_UNGREEDY)

This modifier inverts the "greediness" of the quantifiers so that they are 
not greedy by default, but become greedy if followed by ?. It is not compatible
with Perl. It can also be set by a (?U) modifier setting within the pattern 
or by a question mark behind a quantifier (e.g. .*?).

S

When a pattern is going to be used several times, it is worth spending more 
time analyzing it in order to speed up the time taken for matching. If this 
modifier is set, then this extra analysis is performed. At present, studying 
a pattern is useful only for non-anchored patterns that do not have a single 
fixed starting character.

更多信息参见http://www.php.net/manual/en/reference.pcre.pattern.modifiers.php

当您添加/.../g 时,您添加了另一个正则表达式分隔符以及 PCRE 中不存在的修饰符 g,这就是它不起作用的原因.

When you added the / ... /g , you added another regex delimiter plus the modifier g wich does not exists in PCRE, that's why it did not work.

这篇关于使 url regex 全局化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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