Reputation: 1024
I am trying to create a pipe in angular, but I cannot seem to wrap my head around regular expressions.
I am trying to match only lonely or single uppercase letters and split them. Let us say for instance I have the following:
thisIsAnApple
should return [this, Is, An, Apple]
thisIsAString
should return [this, Is, AString]
BB88GGFR
should return [BB88GGFR]
So the plan is to match a capital letter if it is not accompanied by another capital letter.
This is what I have come up with:
const regExr = new RegExp(/(?=[A-Z])(?![A-Z]{2,})/g);
let split = string.split(regExr);
Upvotes: 1
Views: 144
Reputation: 626738
You may use
string.split(/(?<=[a-z])(?=[A-Z])/)
Or, to support all Unicode letters:
string.split(/(?<=\p{Ll})(?=\p{Lu})/u)
Basically, you match an empty string between a lowercase and an uppercase letter.
JS demo:
const strings = ['thisIsAnApple', 'thisIsAString', 'BB88GGFR'];
const regex = /(?<=[a-z])(?=[A-Z])/;
for (let s of strings) {
console.log(s, '=>', s.split(regex));
}
JS demo #2:
const strings = ['thisIsĄnApple', 'этоСТрокаТакая', 'BB88GGFR'];
const regex = /(?<=\p{Ll})(?=\p{Lu})/u;
for (let s of strings) {
console.log(s, '=>', s.split(regex));
}
Upvotes: 2