我想寫一個正則表達式來提取句子中的最後幾個單詞。但是,我似乎無法使其工作。使用正則表達式提取一個句子中的最後幾個單詞?
var str = "Tell me about robin hood";
const TELL_ME_ABOUT_REGEX = /^Tell me about (\w+\s*)*/
if(str.match(TELL_ME_ABOUT_REGEX)){
var matches = TELL_ME_ABOUT_REGEX.exec(str);
console.log("user wants to know about ",matches);
}
我試圖得到了這個詞「羅賓漢」。但我只會以「引擎蓋」而告終。
[ 'Tell me about robin hood',
'hood',
index: 0,
input: 'Tell me about robin hood' ]
我在正則表達式中改變了什麼?
試試這個'告訴我有關([\ w \ s] +)' –