ichachan
ichachan

Reputation: 667

Splitting String Value Using Regex

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.

Upvotes: 0

Views: 43

Answers (1)

nnnnnn
nnnnnn

Reputation: 150080

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.

Upvotes: 2

Related Questions