0

I have a number in JS like 2,5 or 2.5

I want to use a multi split like

'2.5'.split(/,|./)

but it gives me the wrong output, why ?:

["", "", "", ""]

2 Answers 2

3

/,|./ is incorrect regex. It should be in a charatcer class /[,.]/ or be escaped /,|\./

This works: '2.5'.split(/[.,]/)

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

1 Comment

@Bergi - Yes, only dot to be escaped. Corrected.
3

Escape the . regex metacharacter

'2.5'.split(/,|\./)

1 Comment

Whoops, got ahead of myself.

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.