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