var mystring = 'Hello test1 "test2 but one word" test3 test4 ';
var mystring_ar= mystring.split(/\s+/);
I have this /\s+/ expression so I can separate from spaces but my problem is I want to separate from spaces and regular expression should not separate word if words surrounded by ", like "test2 but one word". For example below:
'Hello test1 "test2 but one word" test3 test4 '.split(regex)
=> ['Hello','test1','test2 but one word','test3','test4']
is that possible?
Edited
I have found half solution but not completed below code separate correctly but only works for words for example if string is a/b/as d , it will not separate it to a/b/as and d,it will separate it to a,b,as,d
keywords = keywords.match(/\w+|"[^"]+"/g);
But I think solution should be with split. It should work like separate from whitespaces and don't separate inside of ""
This works easier with match. To match any non-white-space, you can use the opposite of \s, i.e. \S. As quotes also match with \S, you then also need to first test for the quote, and only put the \S as alternative of that. Maybe you also want to ignore a quoted part, when it is followed immediately by a non-space, like "thi"s word" should maybe not become ['thi','s','word'], but['"thi"s','word']. That you can do with negative look ahead: (?!\S). All together that becomes:
/"[^"]*"(?!\S)|\S+/g