Replace words from dictionary with link using regex
题
So i have this html:
<img src="images" alt="alt" />
alt <a href ="http://google/something">alt</a>
test hallo world monkey
<p>alt</p>
and a dictionary containing
{alt, test, hallo, world, monkey, something}
so i need a regex or another method to replace words that are not within a A tag or a img tag I have tryed the following regex:
(?<![a-zA-ZåøæÅØÆ])alt(?![a-zA-ZåøæÅØÆ])^*(?!=)$
解决方案
You could use regex and do a negative lookahead and lookbehind for letters:
(?<![a-zA-Z])keyword(?![a-zA-Z])
in your example this would look like this:
bodyText = Regex.Replace(bodyText, "(?<![a-zA-Z])" + article.headword + "(?![a-zA-Z])", "<a class=\"dic\" href=\"#\">" + article.headword + "</a>");
My first intend was to do a positive search for whitespace characters, but then I thought of punctuation and stuff like this, a keyword is still a keyword if it has a .,!? at the end, right? So lookaheads and lookbehinds essentially check if something preceeding or succeeding your keyword, without replacing these, too.
其他提示
This is what i ended up doing
var regex = new Regex("(?<![a-zA-Z" + SpecialChars + "])" + article.headword + "(?![a-zA-Z" + SpecialChars + "])+(?!==)");
bodyText = regex.Replace(bodyText, "<a href=\"#dic\">" + headword + "</a>");
This will only replace the first one
不隶属于 StackOverflow