arrays - How do I tokenize a string in Javascript? -



arrays - How do I tokenize a string in Javascript? -

i have string:

'"apples" , "bananas" or "gala melon"'

i convert array

arr = ['"apples"', 'and', '"bananas"', 'or', '"gala melon"']

i don't know if can regular expression. i'm origin think may have parse each character @ time match double quotes.

input = '"apples" , "bananas" or "gala melon"' output = input.match(/\w+|"[^"]+"/g) // output = ['"apples"', 'and', '"bananas"', 'or', '"gala melon"']

explanation regex:

/ - start of regex \w+ - sequence of word characters | - or "[^"]+" - quoted (assuming no escaped quotes) /g - end of regex, global flag (perform multiple matches)

javascript arrays string

Comments

Popular posts from this blog

web services - java.lang.NoClassDefFoundError: Could not initialize class net.sf.cglib.proxy.Enhancer -

Accessing MATLAB's unicode strings from C -

javascript - mongodb won't find my schema method in nested container -