最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Regex: Match desired chars at the beginningend of string - Stack Overflow

programmeradmin2浏览0评论

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.

  1. So if I have 'AB Some AB pany name AB', it should return 'Some AB pany name'.
  2. 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.

  1. So if I have 'AB Some AB pany name AB', it should return 'Some AB pany name'.
  2. 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 a B. 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
Add a ment  | 

1 Answer 1

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
发布评论

评论列表(0)

  1. 暂无评论