开发者

How can I fix this regex to allow a specific string?

This regex comes from Atwood and is used to filter out anchor tags with anything other than the href and a title:

 <a\shref="(\#\d+|(https?|ftp)://[-A-Za-z0-9+&@#/%?=~_|!:,.;]+)"(\stitle="[^"]+")?\s?>

I need to allow am additional attribute that specifically matches: target="_blank". So the following url should be allowed:

 <a href="http://www.google.com" target="_blank">

I tried changing the pattern to these:

 <a\shref="(\#\d+|(https?|ftp)://[-A-Za-z0-9+&@#/%?=开发者_如何学运维~_|!:,.;]+)"(\stitle="[^"]+")(\starget="_blank")?\s?>
 <a\shref="(\#\d+|(https?|ftp)://[-A-Za-z0-9+&@#/%?=~_|!:,.;]+)"(\stitle="[^"]+")(\starget=\"_blank\")?\s?>

Clearly I don't know regex very well. How should the pattern be adjusted to allow the blank target and no other targets?


<a\shref="(\#\d+|(https?|ftp)://[-A-Za-z0-9+&@#/%?=~_|!:,.;]+)"\s(target=\"_blank\")>

Will do what you are asking.

If you are a regex nub, let me recommend RegExBuddy. It is a program that lets you test your regex's on sample text or sample files.

Saves a lot of time.

http://www.regular-expressions.info/regexbuddy.html (Regex Buddy)

http://www.regular-expressions.info is also a good resource


<a\shref="(\#\d+|(https?|ftp)://[-A-Za-z0-9+&@#/%?=~_|!:,.;]+)"(\stitle="[^"]+")(\starget="_blank")>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜