Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/429.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Javascript在case语句中打开正则表达式比较的结果_Javascript_Regex_Switch Statement - Fatal编程技术网

使用Javascript在case语句中打开正则表达式比较的结果

使用Javascript在case语句中打开正则表达式比较的结果,javascript,regex,switch-statement,Javascript,Regex,Switch Statement,我试图将正则表达式转换为case语句: var url = "news/asdd-asdadas-assas.aspx"; var regex = new RegExp('\/.*?(.aspx)', 'g'); // from '/' to '.aspx' var match = url.match(regex)[0]; // matches everything behind news and returns a string case("/news" + match) ... 我基本上

我试图将正则表达式转换为
case
语句:

var url = "news/asdd-asdadas-assas.aspx";
var regex = new RegExp('\/.*?(.aspx)', 'g'); // from '/' to '.aspx'
var match = url.match(regex)[0]; // matches everything behind news and returns a string

case("/news" + match) ...
我基本上想要匹配的url是
news/asdd asdadas assas.aspx

我有其他匹配的url,为导航添加了一个活动类。URL由页面路由生成

我一直在做这个
var pathName=window.location.pathName

var re = "/[^news][\w\W]+/";
switch (pathName) {
    case "/":
        $('#home-active').addClass('active');
        break;
    case "/about-us.aspx":
        $('#about-active').addClass('active');
        break;
等等

但我想这样做:

case "/news"+re:
        $('#news-active').addClass('active');
        break;    
我已经尝试了许多不同的正则表达式,我是否正确地使用了
案例


如何通过连接
re
变量来匹配url

您可以使用正则表达式的测试方法,如果匹配,则返回true

  var re = /[^news][\w\W]+/;

      case "/news".match(re)[0]:
          $('#news-active').addClass('active');
          break; 

您可以使用正则表达式的测试方法,如果匹配,则返回true

  var re = /[^news][\w\W]+/;

      case "/news".match(re)[0]:
          $('#news-active').addClass('active');
          break; 

这不是在
Javascript
中使用正则表达式的正确方法。正则表达式匹配返回true/false。这意味着在你的案例陈述中,你最终会得到

pathName===pathName.match(re)

也就是说

pathName===true
pathName===false

如果
pathName
不是
null
undefined
,它实际上可能会进行计算,但几乎肯定不会导致您期望的行为

EDIT:根据@JonathanM提供的引用,当传递到开关的参数计算为true或false时,case语句几乎肯定不会计算

<>你会考虑修改你的代码使用<代码> .Math()<代码>方法?< /p>
if(pathName.match(re)){
    $('#home-active').addClass('active');
}else if(pathName.match(re2)){
    $('#about-active').addClass('active');
}

这不是在
Javascript
中使用正则表达式的正确方法。正则表达式匹配返回true/false。这意味着在你的案例陈述中,你最终会得到

pathName===pathName.match(re)

也就是说

pathName===true
pathName===false

如果
pathName
不是
null
undefined
,它实际上可能会进行计算,但几乎肯定不会导致您期望的行为

EDIT:根据@JonathanM提供的引用,当传递到开关的参数计算为true或false时,case语句几乎肯定不会计算

<>你会考虑修改你的代码使用<代码> .Math()<代码>方法?< /p>
if(pathName.match(re)){
    $('#home-active').addClass('active');
}else if(pathName.match(re2)){
    $('#about-active').addClass('active');
}

您的解决方案不起作用,因为您在case语句中使用了regex。必须匹配正则表达式,然后将其放入case语句中:

var url = "news/asdd-asdadas-assas.aspx";
var regex = new RegExp('\/.*?(.aspx)', 'g'); // from '/' to '.aspx'
var match = url.match(regex)[0]; // matches everything behind news and returns a string

case("/news" + match) ...

您的解决方案不起作用,因为您在案例陈述中使用了正则表达式。必须匹配正则表达式,然后将其放入case语句中:

var url = "news/asdd-asdadas-assas.aspx";
var regex = new RegExp('\/.*?(.aspx)', 'g'); // from '/' to '.aspx'
var match = url.match(regex)[0]; // matches everything behind news and returns a string

case("/news" + match) ...

不能在case子句中使用regex。case子句中的值必须与switch语句中的表达式完全匹配(==)。请参见此处:您不能通过连接
re
变量来匹配url。您不能在case子句中使用regex。case子句中的值必须与switch语句中的表达式完全匹配(==)。请参见此处:您无法通过连接
re
变量来匹配url。这将不起作用。如果值为true,则不会执行case语句。如果该值与switch语句中表达式的值完全匹配(==),则执行该命令。这行不通。如果值为true,则不会执行case语句。如果该值与switch语句中表达式的值完全匹配(==),则执行该命令。