const regex = /((?<=href="\/)(.+(?=\.(doc|pdf)))|(?<=src=")((http|https).+(?=\/(v2|userfiles)\/)))/g;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('((?<=href="\\\/)(.+(?=\\.(doc|pdf)))|(?<=src=")((http|https).+(?=\\\/(v2|userfiles)\\\/)))', 'g')
const str = `href="http://www.mataderomadrid.org/v2/userfiles/WEB_ESP_LINEAS%20DE%20PROGRAMACION%20DE%20MATADERO_29_01_19.pdf"
href="/userfiles/WEB_ESP_LINEAS%20DE%20PROGRAMACION%20DE%20MATADERO_29_01_19.pdf"
href="/userfiles/WEB_ESP_LINEAS%20DE%20PROGRAMACION%20DE%20MATADERO_29_01_19.doc"
href="/this/is/valid"
href="/this/another/valid/link.html"
src="/userfiles/image.png"
src="/userfiles/image.jpeg"
src="/userfiles/image"
src="http://www.mataderomadrid.org/v2/xxx.jpeg"
src="http://www.mataderomadrid.org/userfiles/xxx.jpeg"
`;
// 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