开发者

.Net Regex to highlight keywords including special characters

Keywords highlighters that are available on the internet do not highlight special characters. e开发者_运维百科.g. http://sites.google.com/site/yewiki/aspnet/highlighting-multiple-search-keywords-in-aspnet

How can I make them hightlight any characters. e.g. C++


The code in the example is pretty much just taking the search string as the Regex and replacing the spaces with the or operator(|). Special characters entered will be misinterpreted as Regex operators. Much like the code exaple does a .Replace(" ", "|") , you can do a series of replaces like .Replace("@", "\@") to make sure the specials are escaped in the Regex and not interpreted as there special meaning. I'm not sure exactly what you are after, but you could also just append the "\@" or whatever specials you are looking for to the Regex expression. I assume if you are doing a C++ like code highlighter your Regex will be a constant, and not a typed in search string like the example you gave.


Well first you need to get clear what you mean by "highlight any characters".

Do you want to highlight all characters that aren't letters or numbers? Or do you want in the case of C++ to highlight the whole word?

Once you've got it straight, you can use a regex table like this one to work out a suitable regex for matching.

Or better still you can re-use something like syntax-highlighter or google-code-prettify

There's also a well-written article on codingthewheel.com that may be helpful to you.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜