问题描述
我的后端有一个所见即所得的编辑器,它运行了我写的第一个正则表达式.这是在 PHP4 中,使用 preg_replace()
.我正在捕获 URI 和链接文本.
I have a wysiwyg editor in my back end, and it is tripping up the first regular expression I wrote. This is in PHP4, using preg_replace()
. I'm capturing the URI and linked text.
@<a\shref=\"http[s]?://([^\"]*)\"[]>(.*)<\/a>@siU
客户希望在新窗口中打开所有外部链接,这就是我用来查找所有(希望是)外部链接,但保留内部链接、页面锚链接等的表达方式
The client wanted all external links to open in a new window, so that's the expression I was using to find all (hopefully) external links, but leave internal, page anchor links, etc
我意识到如果用户在链接上选择粗体,所见即所得编辑器还会添加 style="font-weight: bold"
.我最近才开始学习正则表达式,所以我不确定如何解决这个问题.
I realised the wysiwyg editor also adds style="font-weight: bold"
if the user selects bold on the link. I've only recently started learning regular expressions so I'm unsure how to go about this problem.
我该怎么做?
推荐答案
这应该是匹配的:
/<a\s+([^>]*)href="https?:\/\/([^"]*)"(.*?)>(.*?)<\/a>/
这里有用的是惰性匹配.*?
这意味着它只会匹配它绝对需要的数量,而不是常规匹配,这是贪婪的.
The useful thing here is the lazy match. *?
it means that it'll match only as much as it absolutely needs to, as opposed to the regular match, which is greedy.
为了演示,用这段文字:
To demonstrate, with this text:
a b c d a b c d
这些正则表达式会有不同的结果:
these regexes will have different results:
/a.*c/ selects: "a b c d a b c"
/a.*?c/ selects: "a b c"
这篇关于正则表达式 - 匹配所有具有可选属性的锚点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!