Su Beng Keong
Su Beng Keong

Reputation: 1034

Regular Expression replacing keyword that occurred multiple times

I tried to match the "text" word so that i can replace and highlight the "text" word, using..

strText = "this is some triple text text texting word"
Keyword = " text "
Regex.Replace(strText, Keyword, m=> String.Format(" <span class='keywordHighlight'>{0}</span> ");

The result is : this is some triple text text texting word

But only the first word highlighted, i need the space at the begin and end of the "text" keyword to avoid matching "texting",

Anyone have any idea how to solve it?

Upvotes: 0

Views: 34

Answers (1)

Amit Joki
Amit Joki

Reputation: 59262

Use word boundaries:

Regex.Replace(@"\b"+strText+"\b" ...);

\b is a word boundary in regex. It will only match text and not texting

Upvotes: 1

Related Questions