Javascript 匹配字符串中的正则表达式
我正在尝试使用JavaScript中的正则表达式匹配页面位置。基本上,我想检查字符串是否是Javascript 匹配字符串中的正则表达式,javascript,regex,Javascript,Regex,我正在尝试使用JavaScript中的正则表达式匹配页面位置。基本上,我想检查字符串是否是/dashboard+任何字符,例如/dashboard,/dashboard/activity/,/dashboard/myaccount 我试着用*:/dashboard${/*/}做这件事,但没有成功 我该怎么做 提前感谢 const regex=/\/仪表板(\/\w+)*/; const test=“/dashboard/hello”。匹配(正则表达式) const testA=“/dashbo
/dashboard
+任何字符,例如/dashboard
,/dashboard/activity/
,/dashboard/myaccount
我试着用*
:/dashboard${/*/}
做这件事,但没有成功
我该怎么做
提前感谢
const regex=/\/仪表板(\/\w+)*/;
const test=“/dashboard/hello”。匹配(正则表达式)
const testA=“/dashboard/hello/world”。匹配(regex)
const testB=“/dashboard.match(正则表达式)
const testC=“/hello/world.match(正则表达式)
控制台日志(测试);
控制台日志(testA);
log(testB);
log(testC)代码>
const regex=/\/仪表板(\/\w+)*/;
const test=“/dashboard/hello”。匹配(正则表达式)
const testA=“/dashboard/hello/world”。匹配(regex)
const testB=“/dashboard.match(正则表达式)
const testC=“/hello/world.match(正则表达式)
控制台日志(测试);
控制台日志(testA);
log(testB);
log(testC)
您只是想看看目标字符串是否以/dashboard
开头吗?如果是这样,正则表达式就是/^dashboard/
。我尝试了location.pathname!==/^dashboard/
但它不起作用您的意思是/^dashboard/.test(location.pathname)
?/^dashboard/.test(location.pathname)
这不起作用。它也只接受/
不,它不是。另外,发布一个。您只是想看看目标字符串是否以/dashboard
开头吗?如果是这样,正则表达式就是/^dashboard/
。我尝试了location.pathname!==/^dashboard/
但它不起作用您的意思是/^dashboard/.test(location.pathname)
?/^dashboard/.test(location.pathname)
这不起作用。它也只接受/
不,它不是。另外,发布一个。!location.pathname.match(/\/dashboard(\/\w+)*/g
工作得很好。谢谢!/g是全局的你可以删除它是的我删除了它。谢谢!!location.pathname.match(/\/dashboard(\/\w+)*/g
工作得很好。谢谢!/g是全局的你可以删除它是的我删除了它。谢谢!