简体   繁体   English

匹配字符串中的正则表达式

[英]Match Regular Expression in string

I'm trying to match a page location using regular expressions in JavaScript. 我正在尝试使用JavaScript中的正则表达式来匹配页面位置。 Basically, I want to check if the string is /dashboard + any character, eg, /dashboard , /dashboard/activity/ , /dashboard/myaccount ... 基本上,我想检查字符串是否为/dashboard +任何字符,例如/dashboard/dashboard/activity//dashboard/myaccount ...

I tried to do it using .* : /dashboard${/.*/} but got no luck. 我尝试使用.*来做到这一点:/ /dashboard${/.*/}但是没有运气。

How should I do it? 我该怎么办?

Thanks in advance 提前致谢

在此处输入图片说明

 const regex = /\\/dashboard(\\/\\w+)*/; const test = "/dashboard/hello".match(regex) const testA = "/dashboard/hello/world".match(regex) const testB = "/dashboard".match(regex) const testC = "/hello/world".match(regex) console.log(test); console.log(testA); console.log(testB); console.log(testC); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM