regex - How do I split a string with multiple separators in javascript? -
how split string multiple separators in javascript? i'm trying split on both commas , spaces but, afaik, js's split function supports 1 separator.
pass in regexp parameter:
js> "hello awesome, world!".split(/[\s,]+/) hello,awesome,world!
edited add:
you can last element selecting length of array minus 1:
>>> bits = "hello awesome, world!".split(/[\s,]+/) ["hello", "awesome", "world!"] >>> bit = bits[bits.length - 1] "world!"
... , if pattern doesn't match:
>>> bits = "hello awesome, world!".split(/foo/) ["hello awesome, world!"] >>> bits[bits.length - 1] "hello awesome, world!"
Comments
Post a Comment