[Solved] How can I get “first” and “second” with a JavaScript regular expression in “This is my first sentence. This is my second sentence.”? [closed]


Try this pattern \w+(?=(\s+)?sentence)

Demo regex

  1. Positive Lookahead (?=(\s+)?sentence)
  2. 1st Capturing Group (\s+)?
    ? Quantifier — Matches between zero and one times, as many times as possible, giving back as needed (greedy)
  3. \s+ matches any whitespace character (equal to [\r\n\t\f\v ])
  4. + Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)
    sentence matches the characters sentence literally (case insensitive)
var a="This is my first sentence.This is my second sentence"

console.log(a.match(/\w+(?=(\s+)?sentence)/ig))

Updated regex do with while loop and push the value to array

var s="</span><span>wanna-string-a</span></a></span><span>wanna-string-b</span></a></span><span>wanna-string-c</span></a>";

var qualityRegex = /<span>(.*?)<\/span>/g;
var matches;
var qualities = [];

while (matches = qualityRegex.exec(s)) {
  qualities.push(matches[1]);
}

console.log(qualities)

6

solved How can I get “first” and “second” with a JavaScript regular expression in “This is my first sentence. This is my second sentence.”? [closed]