const regex = /(https?:\/\/[a-z0-9]+(?:[\-\.]{1}[a-z0-9]+)*\.[a-z]{2,6})(?=https?)(\S+)/gmi;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('(https?:\\\/\\\/[a-z0-9]+(?:[\\-\\.]{1}[a-z0-9]+)*\\.[a-z]{2,6})(?=https?)(\\S+)', 'gmi')
const str = `hi, this is your link (but this one is bad formatted and useless):
https://www.test.comhttps://app.test.com/a/b/c/5e20bed422e7880012ba8acc/next?param=1?locale=2
but there is a good link too:
https://app.test.com/a/b/c/5e20bed422e7880012ba8acc/next?param=1?locale=2
and there are also other irrelevant links:
http://www.google.com
http://test.test.com`;
const subst = ``;
// 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