I'm currently working on a TypeScript pipe to split a PascalCase string, but I also want it to split on digits and consecutive capital letters. The challenge is that my current implementation only works in Chrome because Firefox doesn't support lookbehinds. How can I achieve this without using lookbehinds?
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;
}
For example, the string ANet15Amount
should be transformed into A Net 15 Amount
. Note that the regex mentioned above can also handle camelCase strings.