给出句子the description is about fast cars
。和一个字符串str = description is
,我怎么能得到“关于”这个词?
我尝试了一下正则表达式后面但却无法想出一个优雅的方式。任何其他建议然后使用indexOf几次以及拆分拼接? THX
let re = new RegExp('(?<=' + str + ').*');
let result = mySentence.match(re)[0].split(' ')[0];
修改
我忘了添加str
匹配应该不区分大小写。
答案 0 :(得分:2)
没有必要前瞻。
let s = "the description is about fast cars"
let f = "description is";
let r = new RegExp(f + "\\s(\\w+)");
console.log(s.match(r)[1]);
&#13;
答案 1 :(得分:0)
试试这个:
var sentence = "the description is about fast cars";
var word = "description is";
function getFirstAfterSplitSentence(sentence, word) {
return sentence.split(word)[1].split(" ")[1];
}
console.log(getFirstAfterSplitSentence(sentence, "is"));
答案 2 :(得分:0)
一般方法是:
const orig = 'the description is about fast cars';
const str = 'description is';
const result = orig.split(str).pop().trim().split(' ').shift();
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }