const regex = /((?:\/[A-Za-z0-9-]+)+)?(\/tag\/[A-Za-z0-9-%]+)+(\/.*)?/g;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('((?:\\\/[A-Za-z0-9-]+)+)?(\\\/tag\\\/[A-Za-z0-9-%]+)+(\\\/.*)?', 'g')
const str = `http://example.com/cat1/subcat3/subcat4/tag/this%20is%20page/asdasda?start=130/asdasdasd
http://example.com/cat1/subcat3/subcat4/tag/this%20is%20pageasdasd
example.it/news/tag/this%is%20n%page?adsadsadasd
http://example.com/tag/thispage/asdasdasd.-?asds=
http://example.com/tag/this%20is%20page/asdasd
http://example.com/tag/this
`;
const subst = `$2`;
// The substituted value will be contained in the result variable
const result = str.replace(regex, subst);
console.log('Substitution result: ', result);
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