1

My specific question is I want to split an string using javascript split function like

my input string is:- "Andy and sandy" are going

and I am expecting my output in an array like

var Result= ["Andy and sandy","are","going"];

My script will be like

var str='"Andy and sandy" are going';
var Result=str.split(RegularExp);//what would be the RegularExp ?

Or is there any other way please help me out.

Thanks!

Anand Jha
  • 10,404
  • 6
  • 25
  • 28
  • 2
    Duplicate : http://stackoverflow.com/questions/2817646/javascript-split-string-on-space-or-on-quotes-to-array – Sheetal Nov 25 '13 at 11:12
  • @Sheetal , intentionally I haven't posted this question for wrong means, By the way thanks for given link. – Anand Jha Nov 25 '13 at 11:30
  • Assuming that what you want goes beyond the previous Question, it needs to be specified better than giving a single example. Why do you need this result, and how does it fit into your application's requirements? – hardmath Nov 25 '13 at 12:30
  • @hardmath , basically what I am trying is to implement search for an app with user input and if input will have word(s) under quote then we will need to search the exact word and for your information input might have AND,OR,NOT keywords which I will treat like a logical operator...hope you will now understand how this is going to help me. – Anand Jha Nov 25 '13 at 13:21

1 Answers1

2

The regular expression could be:

/ (?=(?:[^"]*"[^"]*")*[^"]*$)|"/

You still have to filter out the empty splits (due to " being at the start of the string):

var str = '"Andy and sandy" testing does work " lol is a nice word" are going';
var Result = str.split(/ (?=(?:[^"]*"[^"]*")*[^"]*$)|"/)
                .filter(function(item) { return item !== '' });

// ["Andy and sandy", "testing", "does", "work", " lol is a nice word", "are", "going"]

The regexp matches all " and aditionally any space that is followed by an even number of ".

Tibos
  • 27,507
  • 4
  • 50
  • 64