I’m given a horribly delimited string of ids. Each id is surrounded by a space(‘ ‘) on the left and a carriage return on the right(‘\r’). I want to split this string and get back all the ids in javascript.
For example:
' hey\r my\r name\r is\r' should return array of ['hey', 'my', 'name', 'is']
So far I tried:
str.split(/ (.+)\r/g)
str.split(/(?:\s)(.+)(?:\r)/)
str.split(/\s(.+?=\r)/)
All but to no avail. I got the closest with str.split(/(?:\s)(.+)(?:\r)/), but it gives me:
['', 'hey', '', 'my', '', 'name', '', 'is', ''].
What is matching the empty strings?
What is the regex to match this?
EDIT:
Id can have any other whitespace char, so this is a valid id: 123\t234\n. and
‘ abc\r 123\n456\t\r’ should return [‘abc’, ‘123\n456\t’ ]
Thanks!
How about:
that is, remove the leading and trailing white space characters and then just split on inner white space characters (and consume them all).
If you only want to split on
\rthen:and you can change the trimming to: