I need a regex to match exactly 'AB'
chars set at the beginning or at the end of the string and replace them with ''
. Note: it should not match parts of that chars set, only if it occurs whole.
- So if I have
'AB Some AB company name AB'
, it should return 'Some AB company name'
.
- If I have
'Balder Storstad AB'
, it should remove only 'AB'
and not the 'B'
at the beginning because it is not whole 'AB'
, only the part of it.
What I tried is:
name.replace(/^[\AB]+|[\AB]+$/g, "");
And it is OK until single "A" or "B" encountered at the beginning or end of the string. If test string is 'Balder Storstad AB'
it matches both 'B'
at the beginning and 'AB'
at the end and returns 'alder Storstad'
. It should skip single 'B'
or single 'A'
at the beginning or end.
What is wrong in my regex?
EDIT:
I forgot to add this. If test strings are:
"ABrakadabra AB" or "Some text hahahAB" or "ABAB text text textABAB"
"AB" should not be matched because they are not separate "AB" groups but part of other word.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…