且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

用< A>替换非HTML链接标签

更新时间:2023-10-28 22:49:16

添加代码到捕获开始锚标记的模式的开头,然后在捕获到的东西时不执行回调代码:

Add code to the beginning of the pattern to capture an opening anchor tag, and then do not perform the callback code when it has captured something:

/(<a[^>]*>)?http:\/\/([,\%\w.\-_\/\?\=\+\&\~\#\$]+)/

然后你需要在你的lamda函数中添加一个if来查看$ matches中是否有任何内容[1](不要忘记增加你的捕获量)

You will then need to add an if to your lamda function to see if there is anything in $matches[1] (Don't forget to increment your captures as well)

你不能使用由于捕获不是一个固定的长度,所以负面看待断言,但你可以使用一个负向前看断言来结束标记,这样它就会丢掉整个匹配:

You cannot use a negative look behind assertion here as the capture is not a fixed length, but you could use a negative look ahead assertion for the closing tag so it drops the entire match:

/(<a[^>]*>)?http:\/\/([,\%\w.\-_\/\?\=\+\&\~\#\$]+)(?!<\/a>)/