const regex = /((?:\S+[\t\r\f ]*){1,3}).*/gmi;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('((?:\\S+[\\t\\r\\f ]*){1,3}).*', 'gmi')
const str = `Examples of three words capture
2 3 5-trifenil tetrazolio cloruro
2-propanol para análisis emsure
2,6-diclorofenol indofenol (sal sódica) p.a.
4 nitrofenil fosfato sal sódica
ablanda carnes
acaí en povo al 70%
aceite de almendras
aceite de canola en polvo
aceite de girasol
aceite de hierbabuena
aceite de inmersión
aceite de menta
aceite de naranja
aceite de parafina
aceite de romero
aceite esencial de árbol de té
aceite esencial de caléndula
aceite hidrosolubre de baobab
aceite mineral
aceite mineral con fragancia almendra`;
// 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