Igor Skoldin Igor Skoldin - 1 month ago 6x
Javascript Question

Regex: find out if the symbol is surrounded with equal symbols

I need a JS regex that will find matches of a word character that are not surrounded by any equal word characters. For example, if we are looking for b it should match aba or ubu and should not abc or bca.

Any characters in the given string are lowercase English letters, which are alternate (e.g. we can have ababa but cannot bbaa).

I tried using lookarounds like this:


But I did not manage to figure out how do I replace the a so that we know that it is the same symbol that is on the left and the right side of
. Any insights are appreciated.


Using below regex:


You are able to match these kind of characters. If you need to manipulate surrounded bs then you can code it like this:

> "aabb aba".replace(/(.)(b)(?=\1)/g, function(match, p1, p2) {
    return p1 + 'c';
< "aabb aca"