2

I'm using Javascript regex functions to split expressions like:

var1=32
var2<var4
var1!=var3

I'm using this regex:

/^(.*)([=|!=|<|>])(.*)$/ig

It works pretty well except with the != (different) operator. What's the problem?

1
  • [] denotes a character class, meaning this part of your expression is essentially the same as [=|!<>]. Commented Sep 25, 2011 at 9:05

1 Answer 1

6

The problem is you are using a character class instead of an alternation. The expression [=|!=|<|>] is equivalent to [<>=!|]. Remove the [ and ] to get what you want.

/^(.*?)(!=|=|<|>)(.*)$/ig

I've also changed the first (.*) to be non-greedy so that it doesn't consume the ! in !=. If you know that the left and right expressions will always contain (for example) only alphanumeric characters only, it would be better to specify that instead of matching with the dot.

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

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.