0

I'm doing a regular expression for a linking system, and the syntax looks like this:

<a href=":login">Login</a>

This tells the system that this link should be converted to JS or an HTML destination depending on the user's browser capabilities.

Right, so I have all the back-end stuff working fine, but I noticed a strange problem with the regular expresion that I'm using to catch these types of links. When a dynamic link (href=":) stands by itself (i.e. not next to another object) then it works fine; however, if a dynamic link like

<a href=":myLink">

comes after a standard link like

<a href="myLink">

then the dynamic link doesn't get altered, like it should.

Here is a codepad link to some sample code that demonstrates the bug. http://codepad.org/ZKdm2NkS

Notice the <a href=":first"> link does not get modified but the <a href=":second"> link does.

I'm not very good with regexps so I'm sure there's a better way of handling things rather than just using a (.*) everywhere you turn, but like I said, I'm open to better ideas and opinions.

4

1 回答 1

1

因为您要替换的唯一内容是“:myLink”部分,您实际上并不需要匹配其余部分......试试这个:

$html = preg_replace('/href=":([\w]+)"/', 'href="processedLink-$1"', $html);

这仅匹配单词 (\w) 字符(字母、数字、下划线)

于 2013-05-24T18:49:15.997 回答