javascriptregexreplaceword-boundaries

Non-capturing group matching whitespace boundaries in JavaScript regex


I have this function that finds whole words and should replace them. It identifies spaces but should not replace them, ie, not capture them.

function asd (sentence, word) {
     str = sentence.replace(new RegExp('(?:^|\\s)' + word + '(?:$|\\s)'), "*****");
     return str;
};

Then I have the following strings:

var sentence = "ich mag Äpfel";
var word = "Äpfel";

The result should be something like:

"ich mag *****" 

and NOT:

"ich mag*****"

I'm getting the latter.

How can I make it so that it identifies the space but ignores it when replacing the word?

At first this may seem like a duplicate but I did not find an answer to this question, that's why I'm asking it.

Thank you


Solution

  • You should put back the matched whitespaces by using a capturing group (rather than a non-capturing one) with a replacement backreference in the replacement pattern, and you may also leverage a lookahead for the right whitespace boundary, which is handy in case of consecutive matches:

    function asd (sentence, word) {
         str = sentence.replace(new RegExp('(^|\\s)' + word + '(?=$|\\s)'), "$1*****");
         return str;
    };
    var sentence = "ich mag Äpfel";
    var word = "Äpfel";
    console.log(asd(sentence, word));

    See the regex demo.

    Details

    NOTE: If the word can contain special regex metacharacters, escape them:

    function asd (sentence, word) {
         str = sentence.replace(new RegExp('(^|\\s)' + word.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&') + '(?=$|\\s)'), "$1*****");
         return str;
    };