I have a string of comma separated values like
let options = '1, FIRST,2, SECOND,3, THIRD,4, FOURTH'
Requirement: There will be a space after first comma which is next to number. I need to split comma only after each word.
I need to convert this options to array in Angular html to show it in dropdown
<mat-select # multiple>
<mat-option *ngFor="let option of options.split(',')" [value]="option">{{option }}</mat-option>
</mat-select>
Expected result:
1, FIRST
2, SECOND
3, THIRD
4, FOURTH
What I got with split(,) is
1
FIRST
2
SECOND
3
THIRD
4
FOURTH
Updated Question: I tried the solution provided here,
let options = '1, FIRST,2, SECOND,3, THIRD,4, FOURTH';
let splitOptions = options.split(/,(?=\d)/);
let splitOptionsWithQuotes = options.split('/^,(?=\d)$/');
for (let options of splitOptions) {
console.log(options);
}
for (let options of splitOptionsWithQuotes) {
console.log(options);
}
options.split(/,(?=\d)/) is working in TypeScript compiler.
Output:
[LOG]: "1, FIRST"
[LOG]: "2, SECOND"
[LOG]: "3, THIRD"
[LOG]: "4, FOURTH"
options.split('/,(?=\d)/') is not working in HTML or TypeScript compiler. This results in
[LOG]: "1, FIRST,2, SECOND,3, THIRD,4, FOURTH"
Putting regex in single quotes not working in html. I tried executing the same in typescriptlang-org online compiler