const regex = /(?m)(?<type>@param) *\K(?<scalar>(?![\$])\S+)? *\K(?<var>\$\S+) *\K(?<message>\S+.*\S+)/mg;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('(?m)(?<type>@param) *\\K(?<scalar>(?![\\$])\\S+)? *\\K(?<var>\\$\\S+) *\\K(?<message>\\S+.*\\S+)', 'mg')
const str = `--- Copyright @fmargerit ---
/**
* First variation with scalar type
*
* @param Int \$a First parameter to add
* @param Int \$b Second parameter to add
*
*/
/**
* second variation without scalar type
*
* @param \$a first text
* @param \$b second text
* @param \$c third text
*
*/
/**
* Invalid case without message
*
* @param Int \$a
*
*/
/**
* Invalid case : without variable
*
* @param Int coucou
*
*/`;
// 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