1

I want to split my following string with multiple argument like space and / and :

21-10-2015 / 7:49:43 AM

I tried following Regular Expression

str.split(/[:-\/]/)
----------^

But it give me error like SyntaxError: invalid range in character class how to resolve it ?

Sadikhasan
  • 18,365
  • 21
  • 80
  • 122
  • You need to escape the hyphen, `\-`, which normally indicates a range of characters, for example `a-z` or `0-9`. – David Thomas Jan 04 '16 at 10:18
  • `"21-10-2015 / 7:49:43 AM".split(/[\s/:]+/)` and Why are you using `-` in regex when don't intent to split using it? – Satpal Jan 04 '16 at 10:19
  • Possible Duplicate of [Regex - Should hyphens be escaped?](http://stackoverflow.com/questions/9589074/regex-should-hyphens-be-escaped) – Tushar Jan 04 '16 at 10:25

1 Answers1

3

The error is because you're including the hypen as the second character in the Regex which is making the parser believe you're trying to set a range between : and \ - which cannot be made. Place the - first in the set, or escape it. To include a space in the characters, add \s to the Regex.

str.split(/[-:\/\s]/);

Working example

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339