Regex split by quote and plus sign

I am trying to break text from a text field using some characters as operators. I have a grouping operator (") and an AND operator (+), which is very similar to google. So this text:

box +box +"box" "box" "is.a.box" +"is.a.box" +"is a box" 

The following is returned in the text box:

 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 regex that I use:

 /[\+\w]+|"[^"]+"/g 

How can I divide by and + characters together?

+4
source share
3 answers

Look at this:

 str.match(/\+?(?:"[^"]*"|[^\s+]+)/g) 

It will start with + , if possible. Then he will try to match a " . If possible, it will take as many characters as possible, and the final one. If not, he will simply accept as many non-spatial, non + characters as possible.

This is pretty much what you had, except that I took out the optional + before all possible cases.

Another addition. If box"box" should result in two matches between box and "box" , use the following:

 str.match(/\+?(?:"[^"]*"|[^\s+"]+)/g) 
+4
source
 /\+?("[^"]*"|[^\s+])+/g 

The trick is to process quoted strings as if they were single characters. We look at non-whitespace, non-special characters ( [^\s+] ), but we will also pretend that quoted strings ( "[^"]*" ) are one character.

 > 'box +box +"box" "box" "is.a.box" +"is.a.box"'.match(/\+?("[^"]*"|[^\s+])+/g) ["box", "+box", "+"box"", ""box"", ""is.a.box"", "+"is.a.box""] > '"string with spaces" +"extended phrase"'.match(/\+?("[^"]*"|[^\s+])+/g) [""string with spaces"", "+"extended phrase""] > 'box+box'.match(/\+?("[^"]*"|[^\s+])+/g) ["box", "+box"] 
+3
source

The result you want can be achieved by simply dividing into spaces.

You might want to make it a little smarter and narrow down a few spaces using the following:

 myArray = str.split(/[\s]+/g) 

For your input string, this returns the desired array:

 ["box", "+box", "+"box"", ""box"", ""is.a.box"", "+"is.a.box""] 
0
source

Source: https://habr.com/ru/post/1447271/


All Articles