EN
TypeScript - split string using regex
0 points
In this short article, we would like to show how to split some strings using a regular expression as a separator in TypeScript.
Quick solution:
xxxxxxxxxx
1
const phoneNumber: string = '+48 888-444-000';
2
const separator: RegExp = /[ -]+/;
3
4
const parts: string[] = phoneNumber.split(separator);
5
6
console.log(parts);
Output:
xxxxxxxxxx
1
['+48', '888', '444', '000']
Where: /[ -]+/
means multiple spaces, dashes, or mix of them.