I was trying to make a pipe in typescript that would split a PascalCase string, but it would be nice if this would also split on digits as well. I would also like it to split on consecutive capital letters. I have this pipe, which works great, except it only works in Chrome and not Firefox, evidently only Chrome supports look backs. How can accomplish this without look backs?
transform(value: string): string {
let extracted = '';
if (!value) {
return extracted;
}
const regExSplit = value
.split(new RegExp('(?<=[a-z])(?=[A-Z])|(?<=[A-Z])(?=[A-Z][a-z])|(?<=[0-9])(?=[A-Z][a-z])|(?<=[a-zA-Z])(?=[0-9])'));
for (let i = 0; i < regExSplit.length; i++) {
if (i !== regExSplit.length - 1) {
extracted += `${regExSplit[i]} `;
} else {
extracted += regExSplit[i];
}
}
return extracted;
}
So for example a string ANet15Amount
should be transformed into A Net 15 Amount
. This regex above also would split a camelCase string, but that's not necessary to consider.
.replace(/([A-Z]|\d+)/g, " $1").trim();
(?!^)([A-Z]|\d+)
avoids first space and doesn't need trim.