Basically, like if you were to say
var string = 'he said "Hello World"';
var splitted = string.split(" ");
the splitted array would be:
'he' 'said' '"Hello World"'
basically treating the quotation mark’d portion as a separate item
So how would I do this in javascript? Would I have to have a for loop that goes over the string checking if the scanner is inside a set of quotation marks? Or is there a simpler way?
You could use regular expressions:
Basically it searches at first for strings with any characters between quotes (including spaces), and then all the remaining words in the string.
For example
var string = '"This is" not a string "without" "quotes in it"';string.match(/(".*?")|(\S+)/g);
Returns this to the console: