0

I have an input string value as below and would like to split on non-numeric literals like whitespace, characters,new line, comma, period, slashes, backslashes, etc.

For example my input value would be:

var list = 
'123
456 789
1234..5678//999
123aaa456'

I want the output value to be: 123, 456, 789, 1234, 5678, 999, 123, 456

I try to split it using the regex below but it keeps stopping on the second number...

var split= list.split(/[\s\t.,;:]+/);

Could anyone please help me? Thanks in advance.

1
  • 3
    /\D+/ === non-numeric Commented Aug 21, 2014 at 1:39

1 Answer 1

2

Use \D to match any non-numeric character, or \D+ to match one or more such characters together:

var split = list.split(/\D+/);

You said:

I try to split it using the regex below but it keeps stopping on the second number...

var split= list.split(/[\s\t.,;:]+/);

I don't see how it could stop on the second number: that regex would produce the output ["123", "456", "789", "1234", "5678//999", "123aaa456"], because your pattern doesn't include forward slashes or letters.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you this is working. For some reason the javascript was not loaded correctly on the page I'm working on so returned wrong result..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.