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
Post a Comment