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

Javascript: Replace all occurences of ' a ' in a string with ' b ' with a regex - Stack Overflow

programmeradmin5浏览0评论

I have a string like 'a b b b a' and want to replace every 'b' in that string that has a whitespace before and after the character with a different character. How could I do that with a regex? My idea was this:

'x a y a x'.replace(new RegExp(' a ', 'g'), ' b '); // is: x b y b x, should: x b y b x (correct)
'x a a a x'.replace(new RegExp(' a ', 'g'), ' b '); // is: x b a b x, should: x b b b x (wrong)
'x aa x'.replace(new RegExp(' a ', 'g'), ' b '); // is: x aa x, should: x aa x (correct)

But that regex is only working if there is not more than 1 occurence of the character next to another occurence of the character. How could I write the regex to get the right behaviour?

I have a string like 'a b b b a' and want to replace every 'b' in that string that has a whitespace before and after the character with a different character. How could I do that with a regex? My idea was this:

'x a y a x'.replace(new RegExp(' a ', 'g'), ' b '); // is: x b y b x, should: x b y b x (correct)
'x a a a x'.replace(new RegExp(' a ', 'g'), ' b '); // is: x b a b x, should: x b b b x (wrong)
'x aa x'.replace(new RegExp(' a ', 'g'), ' b '); // is: x aa x, should: x aa x (correct)

But that regex is only working if there is not more than 1 occurence of the character next to another occurence of the character. How could I write the regex to get the right behaviour?

Share Improve this question asked Oct 19, 2016 at 11:56 JohnJohn 9854 gold badges14 silver badges30 bronze badges 2
  • a+ instead of only a – mic4ael Commented Oct 19, 2016 at 11:58
  • Regex is reading each character waiting to get ' a ' when it reaches the first one like so: 'x[ a ]a a x' it replaces it and continues on to the next bit 'a a x'. It skips the next 'a' since it doesn't have a space before it, ignoring that 'a'. Thats why you get 'x b a b x' – Some Dinosaur Commented Oct 19, 2016 at 12:05
Add a ment  | 

3 Answers 3

Reset to default 6

Use a lookahead after " a" to match overlapping substrings:

/ a(?= )/g

Or, to match any whitespace, replace spaces with \s.

The lookahead, being a zero-width assertion, does not consume the text, so the space after " a" will be available for the next match (the first space in the pattern is part of the consuming pattern).

See the regex demo

var regex = / a(?= )/g;
var strs = ['x a y a x', 'x a a a x', 'x aa x'];
for (var str of strs) {
 console.log(str,' => ', str.replace(regex, " b"));
}

As your matches are overlapping, you can use zero width assertion i.e a positive lookahead based regex:

str = str.replace(/( )a(?= )/g, "\1b");

RegEx Demo

  • ( )a will match a space before a and capture it in group #1 followed by a
  • (?= ) will assert presence of a space after a but won't consume it in the match.

You can use RegExp with g flag with the replace method. Try this

var mystring = "this is a a a a a a test"
mystring.replace(/ a /g , "b");
发布评论

评论列表(0)

  1. 暂无评论