const regex = /type\s([_A-Za-z]+)\s*=\s*(function|procedure)(.*?cdecl;)\s*var(\s[_A-Za-z]+):\s*([_A-Za-z]+)\s(external.*?;)/gs;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('type\\s([_A-Za-z]+)\\s*=\\s*(function|procedure)(.*?cdecl;)\\s*var(\\s[_A-Za-z]+):\\s*([_A-Za-z]+)\\s(external.*?;)', 'gs')
const str = `
// It's better to create the object with the right prototype from the start.
type TJS_SetPrototype = function (cx: PJSContext; var obj: PJSObject;
var proto: PJSObject):Boolean; cdecl;
var JS_SetPrototype: TJS_SetPrototype external SpiderMonkeyLib name 'SM_SetPrototype';
/// Write access an object's reserved slots
type TJS_SetReservedSlot = procedure (obj: PJSObject; index: uint32;
var v: jsval); cdecl;
var JS_SetReservedSlot: TJS_SetReservedSlot external SpiderMonkeyLib name 'SM_SetReservedSlot';
`;
// 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