Reputation: 6029
Given a sentence the description is about fast cars
. And a string str = description is
, How can i get the word "about"?
I tried some look behind regex but could not come up with a elegant way. Any suggestion other then use the indexOf few times along with split an splice? thx
let re = new RegExp('(?<=' + str + ').*');
let result = mySentence.match(re)[0].split(' ')[0];
edit
I forgot to add that str
match should be case insensitive.
Upvotes: 0
Views: 1088
Reputation: 2756
Try this:
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"));
Upvotes: 0
Reputation: 816
A general approach would be:
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; }
Upvotes: 0
Reputation: 73241
There's no need for a lookahead.
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]);
Upvotes: 2