I am working on a Regular Expression to match JavaScript keywords; my problem arises when trying to match single operators (+ , - , / , * , = , etc..).


I am trying to prevent matching '+' whenever another '+' comes before/after it; however, in doing so, the eventual match includes the character which fulfills the condition of "not being +".

Current RegExp: /([^\+]\+[^\+])\b|\b[^\-]\-[l^\-]|\*|[^\<\>\:\/\=][\/\=][^\/\=]|(\+\+)|(\-\-)|(\=\=)/g

How can I make sure that the '+' sign does not have another '+' immediately next to it - but at the same time only the '+' character itself is matched?

Thanks in advance to anyone who might be able to help me.

1 Answers

Community On

If you use negative lookahead and lookbehind assertions, it should work. The following regex matches a "+" only if it is neither preceded nor followed by a "+".


Negative lookbehinds may not be supported in all browsers though. For more information, refer to this post for possible workarounds: Javascript: negative lookbehind equivalent?