var reg = /^www\.[a-z0-9]+\.com\/[a-z0-9]+\/?$/; // 之所以用[a-z0-9]+而不是\w+是排除如果字符串的如果存在下划线的匹配影响 var str1 = 'www.xxx.com/profile'; var str2 = 'www.xxx.com/profile/'; var str3 = 'www.xxx.com/profile/sdfsdf'; console.log(reg.test(str1)) // true console.log(reg.test(str2)) // true console.log(reg.test(str3)) // false 希望能帮到你, 正则分析在线地址 https://regexper.com/ 感兴趣的话也可以去看我写的正则入门文章
希望能帮到你, 正则分析在线地址 https://regexper.com/
感兴趣的话也可以去看我写的正则入门文章