如何在子目录网站后监听字符串



当有人转到https://website.com/custom/U1BJR09U.U1BJR09U可以是任何字符串,但它需要传递给函数。我应该如何着手实现这一点?

假设所有流量都重定向到一个点,该点为处理URL更改的主文件提供服务,则应该这样做:

const hasCustom = window.location.pathname.match(/^/custom/([0-9A-Z]+)$/);
if (hasCustom && hasCustom[1]) {
const myString = hasCustom[1];
/* Do something with myString here */
}

正则表达式的解释:

  • ^/(转义斜杠(开头
  • 后面是custom
  • 具有/(另一个斜线(
  • 匹配([0-9A-Z]+)数字和大写字母
  • 字符串以数字和大写字母结尾