How to split a string with multiple separators in TypeScript

1 Answer

0 votes
const input: string = "abc,defg;hijk|lmnop-qrst_uvwxyz";

// Match multiple separators: comma, semicolon, pipe, dash, underscore
const result: string[] = input.split(/[,\;\|\-_]/);

result.forEach((word: string): void => {
    console.log(word);
});


 
/*
run:

"abc" 
"defg" 
"hijk" 
"lmnop" 
"qrst" 
"uvwxyz" 
     
*/

 



answered Jul 21, 2025 by avibootz
...