Skip to content Skip to sidebar Skip to footer

Regex To Match All The Strings Between Two Identical Strings

E.g. I have this string -- This -- is -- one -- another -- comment -- I want the matched elements to be 'This', 'is', 'one', 'another', and 'comment' I was trying this regex --\s+

Solution 1:

In general, you need to use a pattern like

STRING([\s\S]*?)(?=STRING|$)

It will match STRING, then capture into Group 1 any zero or more chars, as few as possible, up to the first occurrence of STRING *stopping right before this word** because the (?=...) is a positive lookahead that, being a zero-width assertion, does not consume matched text or end of string.

A generic variation of the pattern is

STRING((?:(?!STRING)[\s\S])*)

It uses a tempered greedy token, (?:(?!STRING)[\s\S])*, that matches any char, 0 or more occurrences, that does not start a STRING char sequence.

To get all the substrings in the current solution, use a lookahead like

/--\s+([\s\S]*?)(?=\s+--)/g
                ^^^^^^^^^

See the regex demo.

Note that [^--]+ matches 1 or more symbols other than a -, it does not match any text that is not equal to --. [...] is a character class that matches a single character. To match any text of any length from one char up to the first occurrence of a pattern, you can rely on a [\s\S]*? construct: any 0+ chars, as few as possible (due to the lazy *? quantifier).

JS demo:

var s = '-- This -- is -- one -- another -- comment --';
var rx = /--\s+([\s\S]*?)(?=\s+--)/g;
var m, res=[];
while (m = rx.exec(s)) {
  res.push(m[1]);
}
console.log(res);

Solution 2:

To read all I would use positive look ahead:

const data = '-- This -- is -- one -- another -- comment --'constreadAll = data => {
  const regex =/--\s*(.*?)\s*(?=--)/gconst found = []
  let temp
  while (temp = regex.exec(data)) {
    found.push(temp[1])
  }
  return found
}

console.log(readAll(data))

And to remove comments just do this:

const data = `-- This -- is -- one -- another -- comment -- this is not a comment`.replace(/--.*--/g, '')

console.log(data)

Post a Comment for "Regex To Match All The Strings Between Two Identical Strings"