1

I'm writing a jQuery plugin to highlight C++ code on my webpages. I'm not really too good with RegEx, so I can't figure out how to highlight keywords (like 'if', 'case', 'void', etc.) without highlighting parts of words (Like the 'int' in 'Paint'). I changed it a bit, but some things still don't get highlighted. This is what my RegEx looks like:

str = str.replace(new RegExp("([' ' || \t ||,|| \"\"])(" + ParserKeywordsArr[i] + ")", 'g'), "$1<span style='color:" + ParserColorsArr[i] + ";'>" + ParserKeywordsArr[i] + "</span>");

This goes through every keyword I want highlighted, which is stored in an array, and theres another array with the colors (Ex. ParserKeywordsArr[0] is void, ParserColorsArr[0] is blue, so it would highlight in blue). However, when I put in something like this:

void CCodeEditorCtrl::OnPaint(WPARAM wParam, LPARAM lParam)
{
    return;
}

Return gets highlighted, but void doesn't. I don't know how to tell RegEx "If its the first word on the line, and it matches a keyword, highlight it"

1
  • 2
    You'll need to get good with Regex for this project... Commented Jan 27, 2012 at 22:00

2 Answers 2

2

I would recommend looking at the source of Alex Gorbatchev's SyntaxHighlighter, which does by the way highlight C++ among other languages. I think you'll also find RegexPal helpful for testing them.

Sign up to request clarification or add additional context in comments.

1 Comment

I'll look into those. I was just using notepad for testing, although I believe JavaScript has different syntax.
1

\b is a word break.

/in/.test('loading'); // => true
/\bin\b/.test('loading'); // => false
/\bin\b/.test('load.in.g'); // => true

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.