Non-capturing group matching whitespace boundaries in JavaScript regex

断了今生、忘了曾经 提交于 2020-01-21 12:20:29

问题


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


回答1:


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

  • (^|\s) - Group 1 (later referred to with the help of a $1 placeholder in the replacement pattern): a capturing group that matches either start of string or a whitespace
  • Äpfel - a search word
  • (?=$|\s) - a positive lookahead that requires the end of string or whitespace immediately to the right of the current location.

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;
};


来源:https://stackoverflow.com/questions/49904525/non-capturing-group-matching-whitespace-boundaries-in-javascript-regex

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!