NKara
NKara

Reputation: 11

How to split a string by multiple separator in Typescript

i need to split a string by semicolon, colon, comma, cr, lf, clfr and whitespace. How can i do that?

Upvotes: 1

Views: 6287

Answers (2)

user8462556
user8462556

Reputation: 536

I would recommend /[,.;/ ]/ regex. It will split with all provided chars between []

"55,22.222;55 555".split(/[,.;/ ]/)

"15/01/2020".split(/[,.;/ ]/)

(Remove last empty space if you don't want to split by space => /[,.;/]/ )

Upvotes: 0

toskv
toskv

Reputation: 31602

You can give the split function a regex to split it by.

A simple one looks like this.

const text = "a,c;d e";

const splitted = text.split(/;|,| /);

Upvotes: 6

Related Questions