const regex = /(?<=[:=!?&,;|{\[\(\n])[\x09\x20]*(?:\/(?![*+?])(?:(?:[^\n\[\\\/]*+(?:\\\\)*+(?:\\\[|\\\/|\\(?!\[))?(\[)?)+(?(1)([^\n\]\\]*+(\\\\)*+(\\\]|\\(?!\]))?)*((?<!\\)\])?))*[^\n]*?(?<![^\\]\/)(?<![^\\]\\)\/)[gim]{0,3}[\x09\x20]*+(?![^}\]\),;:.\n])/g;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('(?<=[:=!?&,;|{\\[\\(\\n])[\\x09\\x20]*(?:\\\/(?![*+?])(?:(?:[^\\n\\[\\\\\\\/]*+(?:\\\\\\\\)*+(?:\\\\\\[|\\\\\\\/|\\\\(?!\\[))?(\\[)?)+(?(1)([^\\n\\]\\\\]*+(\\\\\\\\)*+(\\\\\\]|\\\\(?!\\]))?)*((?<!\\\\)\\])?))*[^\\n]*?(?<![^\\\\]\\\/)(?<![^\\\\]\\\\)\\\/)[gim]{0,3}[\\x09\\x20]*+(?![^}\\]\\),;:.\\n])', 'g')
const str = `// Regular expressions
r = /([\\\\/])?([^\\\\/]*)\$/;
r = /([\\\\/])?([^\\\\:/]*)\$/; // коммент
/abc\\[def\\[^\\s[\\]/]ghi/g.test(s);// здеÑÑŒ одна пара квадратных Ñкобок!
r = /\\//i;
r = /[/\\*]/img; // коммент
r = /[a-z]{5}/;
r = /[a-z]{1,5}/;
r = /([a-z])+/;
r = /([a-z]{1,5})+/;
r = /f([a-z])/;
r = /f([a-z]+)*/;
r = /a(b)c(d)/;
r = /a(b)?c(d)+?/;
r =(t|/\\*/.test(s));
r =(t|/\\*/); // коммент
r =/./.test(s) && f();
r=/[a-z]/.test(s) && f();
var a = [/a\\/, \\/b/];
a=[/a/,/b/];
var o = { r: /./, x: 2 }; // коммент
o={r:/./,x:2};
var x = a && /b/.test(s);
x = a ? /b/ : /c/;// коммент
x=a?/b/:/c/;
x=a?/b\\\\/:/c\\/\\\\/; // коммент
x=a?/b\\\\/:/c\\/\\\\/; // коммент
x = !/a/.test(s);
x = f(/./i);
x=1*s.replace(/^[^\\d]*(\\d+).*\$/,'\$1')/2;
/\\s\\n\\s+\\/\\/\\\\\\\\/.test(s)?f(null):f(/\\\\/);
/\\s\\n\\s+\\/\\/\\\\\\\\/.test(s)?f(null):f(/\\\\fghj[\\[fghj\\]]kl/), /fght/;
a=s.search(/\\s\\n\\s+\\/f\\//gi);
r=/>\\.jjj\\n<\\s\\</
.exec(s);
r=/ \\n \\s/g,
t=/'/g
;
// Not a regular expressions
var n = 1/2;// коммент
n = 1/2/3;
n = 1/2/p;
n = 1/(2/p); // коммент
n = 1/this.n/3;
n = 1/this.f(5+4)/3;
n = 1/this.f(2/3);
n = 1/2 + (function() { return 3/4; })();
n = 1/2*(this.x/3 + 2);
n = 1/2*(this.x + 2/3);
n =p/2,t=3/p;
n = f(1/2) + f(3/2);`;
// Reset `lastIndex` if this regex is defined globally
// regex.lastIndex = 0;
let m;
while ((m = regex.exec(str)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
// 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