const regex = /^(http(s)?:\/\/)?(([\w\.]+)\.(?:com|cn|love|net|com\.cn|org)[\/#!%\w&?\-=~.+]*)$/;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('^(http(s)?:\\\/\\\/)?(([\\w\\.]+)\\.(?:com|cn|love|net|com\\.cn|org)[\\\/#!%\\w&?\\-=~.+]*)$', '')
const str = `https://mp.weixin.qq.com/s?__biz=MzAxNjczMTQxMA==&mid=504131096&idx=1&sn=c2fe41152807821b7916fa9539a0cf87&scene=1&srcid=0718JR98ETFngTl6mDsNRfhK&key=77421cf58af4a65374324bd2f16d7fdd913230b5ab4bd48a72759cc99919893795265ff20c0c8a79c676e636f789899c&ascene=0&uin=MjAzOTExMTUxMg%3D%3D&devicetype=iMac15%2C1+OSX+OSX+10.11.5+build(15F34)&version=11020201&pass_ticket=kA76WNrCKCEZ3JyEii3tYs88BCmLEM%2FI4LPD%2FtHBzoPjYzI9t7seUadtUUVQ9677`;
// Reset `lastIndex` if this regex is defined globally
// regex.lastIndex = 0;
let m;
if ((m = regex.exec(str)) !== null) {
// 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