Javascript 您可以使用正则表达式数组匹配url中的单词吗?

Javascript 您可以使用正则表达式数组匹配url中的单词吗?,javascript,regex,Javascript,Regex,我正在尝试使用正则表达式从URL获取特定的单词。我已将单词作为数组包含,但似乎无法匹配或过滤数组中的任何单词。我总是得到一个错误,即“过滤器”不是一个函数,尽管我似乎和其他人以前一样使用它 函数replaceInput(){ var leadUrl=document.URL; var utm_sources=[/linkedin/,/smartbrief/,/email_paid/,/paid_social/]; var get_source=leadUrl.filter(值=>utm_sou

我正在尝试使用正则表达式从URL获取特定的单词。我已将单词作为数组包含,但似乎无法匹配或过滤数组中的任何单词。我总是得到一个错误,即“过滤器”不是一个函数,尽管我似乎和其他人以前一样使用它

函数replaceInput(){
var leadUrl=document.URL;
var utm_sources=[/linkedin/,/smartbrief/,/email_paid/,/paid_social/];
var get_source=leadUrl.filter(值=>utm_sources.test(值));
var setUtm=获取源;
document.getElementById(“LeadSourceTitle”).placeholder=setUtm;
}

形式测试
使用url的utm填充webform输入


如果我正确阅读了您的代码,那么您是在查找url的路径,而不是url本身。路径可在
location.pathname
中找到。如果没有匹配项,则使用
RegExp.match
传递null,如果找到匹配项,则传递数组(必要时进行筛选)

因此,按照以下思路做一些事情:

const re=/\/linkedin\/\/smartbrief\/\\\/email\u paid\/\124;\/paid\u social\//i;
const path=location.pathname.match(re);
const pathFaked1=“/linkedin/?foo=bar&email\u paid=1”。匹配(re);
const pathFaked2=“/email\u paid/nope/shouldpaynow”。匹配(re);
console.log(路径);
console.log(路径faked1.join(“,”);

console.log(路径faked2.join(“,”)啊,是的,这真的很有帮助,谢谢!让我试试我的URL,看看我得到了什么。我的问题是location.pathname与URL不匹配
var leadUrl=document.URL;const-utm\u sources=/\/linkedin\/\124;\/smartbrief\/\124;\/email\u paid\/\124;\/paid\u social\//i;var path=location.leadUrl.match(utm_源);log(“页面路径为”+leadUrl)