admin管理员组文章数量:1320888
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 pany name AB'
, it should return'Some AB pany 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.
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 pany name AB'
, it should return'Some AB pany 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.
Share Improve this question edited Sep 5, 2017 at 13:30 asked Sep 5, 2017 at 13:07 user3681549user3681549 2-
Square brackets in regex are for character classes: it will match ANY one of the characters listed. In your case, a backslash (for some reason), an
A
, or aB
. By adding the+
quantifier, you are also removing any other matching characters until it finds one that doesn't match. For instance, "ABBBAAA\BBB\ text" would bee " text". – Brian Stephens Commented Sep 5, 2017 at 13:15 - Possible duplicate of Trim specific character from a string – shA.t Commented Sep 5, 2017 at 13:29
1 Answer
Reset to default 6
var rgx = /(^AB\s+)|(\s+AB$)/g;
console.log("AB Some AB pany name AB".replace(rgx, ""));
console.log("Balder Storstad AB".replace(rgx, ""));
console.log("ABrakadabra AB".replace(rgx, ""));
console.log("Some text hahahAB".replace(rgx, ""));
console.log("ABAB text text textABAB".replace(rgx, ""));
Explanation :
(^AB\s+) // AB at the beginning (^) with some spaces after it
| // Or
(\s+AB$) // AB at the end ($) with some spaces before it
本文标签: javascriptRegex Match desired chars at the beginningend of stringStack Overflow
版权声明:本文标题:javascript - Regex: Match desired chars at the beginningend of string - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742009921a2412696.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论