试图将一个变量传递给一个强大的正则表达式,我可以让它在函数之外工作,但不知道如何让它在函数内工作,或者为什么它 match[1] 似乎返回 null。很多关于替换的信息,但不是关于在关键字后查找单词的信息。
这就是我所拥有的
  var s = 'match my word after this word';
  function returnWordAfter(theSentence, theWord){
    var TheRegEx = new RegExp("/"+theWord+"\s(\w*)/");
    var matches = theSentence.match(TheRegEx, '');
    return matches[1];
  }
  var matchedWord = returnWordAfter(s, "this");
  console.log(matchedWord);