title: Javascript: Replace all occurences of ' a ' in a string with ' b ' with a regex
body: <p>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:</p>
  
  <pre><code>'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)
  </code></pre>
  
  <p>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?</p>