I am trying to split a text from a textbox using some characters as operators. I have a grouping operator (“) and a “AND” operator (+), pretty much like google does. So, this text:
box +box +"box" "box" "is.a.box" +"is.a.box" +"is a box"
In the textbox is returning this:
myArray[0] = box
myArray[1] = +box
myArray[2] = +
myArray[3] = "box"
myArray[4] = "box"
myArray[5] = "is.a.box"
myArray[6] = +
myArray[7] = "is.a.box"
myArray[8] = +
myArray[9] = "is a box"
Instead I want it to return this:
myArray[0] = box
myArray[1] = +box
myArray[2] = +"box"
myArray[3] = "box"
myArray[4] = "is.a.box"
myArray[5] = +"is.a.box"
myArray[6] = +"is a box"
This is the regexp I’m using:
/[\+\w]+|"[^"]+"/g
How can I split by ” and + symbols together?
Have a look at this:
That will start with a
+if possible. Then it will try to match a". If it can, it will take as many non-"characters as possible and the final". If there is no"it will just take in as many non-space, non-+characters as possible.This is pretty much what you had, except that I took out an optional
+in front of both possible cases.One more addition. If
box"box"should result in two matchboxand"box"use this: