const regex = /(?xsm) # free-spacing mode, DOTALL, multi-line
(?=.*?blue) # if blue isn't there, fail without delay
########### LINE SKIPPER / COUNTER ############
(?: # start non-capture group
# the aim is to skip lines that don't contain blue
# and capture a corresponding digit sequence
(?: # skip one line that doesn't contain blue
^ # beginning of line
(?:(?!blue)[^\r\n])* # zero or more chars
# that do not start blue
(?:\r?\n) # newline chars
)
# With each line skipped, let Group 1 capture
# an ever-increasing portion of the string of numbers
(?= # lookahead
[^~]+ # skip all chars that are not tildes
( # start Group 1
(?(1)\1) # if Group 1 is set, match Group 1
# (?>\1?) # alternate phrasing for the above
~\d+ # match a tilde and digits
) # end Group 1
) # end lookahead
)*+ # end counter-line-skipper: zero or more times
# the possessive + forbids backtracking
.*? # lazily match any chars up to...
blue # match blue
[^~]+ # match any non-tilde chars
(?(1)\1) # if Group 1 has been set, match it
# \1? # alternate phrasing for the above
~ # match a tilde
\K # drop what we matched so far
\d+ # match digits. This is the match! /;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('(?xsm) # free-spacing mode, DOTALL, multi-line
(?=.*?blue) # if blue isn\'t there, fail without delay
########### LINE SKIPPER \/ COUNTER ############
(?: # start non-capture group
# the aim is to skip lines that don\'t contain blue
# and capture a corresponding digit sequence
(?: # skip one line that doesn\'t contain blue
^ # beginning of line
(?:(?!blue)[^\\r\\n])* # zero or more chars
# that do not start blue
(?:\\r?\\n) # newline chars
)
# With each line skipped, let Group 1 capture
# an ever-increasing portion of the string of numbers
(?= # lookahead
[^~]+ # skip all chars that are not tildes
( # start Group 1
(?(1)\\1) # if Group 1 is set, match Group 1
# (?>\\1?) # alternate phrasing for the above
~\\d+ # match a tilde and digits
) # end Group 1
) # end lookahead
)*+ # end counter-line-skipper: zero or more times
# the possessive + forbids backtracking
.*? # lazily match any chars up to...
blue # match blue
[^~]+ # match any non-tilde chars
(?(1)\\1) # if Group 1 has been set, match it
# \\1? # alternate phrasing for the above
~ # match a tilde
\\K # drop what we matched so far
\\d+ # match digits. This is the match! ', '')
const str = `Paint it white
Paint it black
Why not blue?
Or red or brown?
~1~2~3~4~5~6~7~8~9~10`;
// Reset `lastIndex` if this regex is defined globally
// regex.lastIndex = 0;
let m;
if ((m = regex.exec(str)) !== null) {
// The result can be accessed through the `m`-variable.
m.forEach((match, groupIndex) => {
console.log(`Found match, group ${groupIndex}: ${match}`);
});
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for JavaScript, please visit: https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions