开发者

Regex, how to replace phrases / words not within anchor links?

开发者 https://www.devze.com 2023-04-04 20:16 出处:网络
I\'m upgrading some code written in visual basic. The regex doesn\'t work as I\'d expected. Basically the regex is applied in a loop, it replaces keywords with anchor links. So it builds up a string

I'm upgrading some code written in visual basic. The regex doesn't work as I'd expected.

Basically the regex is applied in a loop, it replaces keywords with anchor links. So it builds up a string which could have lots of phrases / words replaced with links to those phrases / words.

The problem is that if I have two phrases wiki and wikipedia. It will do wikipedia first as I've开发者_StackOverflow中文版 ordered the query to produce the list of phrases by length. However, the regex will replace wikiepedia then wiki reglardless of the whether wikipedia is converted to a link.

So I get a link within a link.

Heres the code.

do 
oRegExp.Pattern = "(" & title & ")(?![^<]*>|[^<]*</a>)"
title = " <a href=""view.asp?id=" guid & """>" & "$&" & "</a> "
content = oRegExp.Replace(content, title)
loop


Adding part of my comment as answer as it seems to have solved it for you.

You can try adding surrounding \b around your title, like "(\b" & title & "\b).

When doing this you do not need to do the replacement in any specific order, as long as you are replacing singe words.

0

精彩评论

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