Peter Huang
Peter Huang

Reputation: 1178

javascript regex how to match this 'And' 'Or'

I have a string something like this:

"q1" = "1" And "q2" = "2" oR "q3" = "3"

I want to split it into a array:

['"q1" = "1"',' And ', '"q2" = "2"',' oR ','"q3" = "3"']

How can I do it with regex?

Thanks!

Upvotes: 0

Views: 51

Answers (1)

anubhava
anubhava

Reputation: 785058

You can use this regex for splitting with a capture group:

/( (?:and|or) )/i

Code:

const str = `"q1" = "1" And "q2" = "2" oR "q3" = "3"`;
var arr = str.split(/( (?:and|or) )/i);

console.log(arr);

Upvotes: 2

Related Questions