gpt4 book ai didi

javascript - 针对引荐来源网址字符串进行正则表达式检查

转载 作者:行者123 更新时间:2023-11-30 06:21:28 27 4
gpt4 key购买 nike

var orig = document.referrer; // Incoming URL
var check = new RegExp("boxes", "gi"); // Literal string, global + case insensitive.

// console.log(check);

if (orig.indexOf(check) > -1) {
console.log('you came from the box section');
} else {
console.log('you DIDNT come the box section');
}

大家好,

我在网站上有一个“盒子”类别,其中所有盒子项目的 URL 中都有“盒子”。来自另一个类别的特定项目需要能够检查用户是否来自“盒子”项目。 (这是一个临时解决方案,因为我只有皮肤级别的访问权限)。

当记录 'check' 时,我得到 '/boxes/gi',它应该在 indexOf 内检查时作为有效的正则表达式字符串工作。

我不太清楚为什么我不能正确检查它,因为结果只是用户不是来自“框”部分。

我有很多东西要学,所以在此之前,我非常感谢任何帮助。

谢谢!

最佳答案

您可以使用字符串变量代替正则表达式

var orig = document.referrer; // Incoming URL

// console.log(check);

if (orig.indexOf("boxes") > -1) {
console.log('you came from the box section');
} else {
console.log('you DIDNT come the box section');
}

关于javascript - 针对引荐来源网址字符串进行正则表达式检查,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52800203/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com