Using Regex in switch in jQuery

2

Well, I have a question here and I do not know if it's possible to do what I want (I think so). The question is, do I have a switch (link) and follow example of cases (example link):

case " link ": break;

case " link ": break;

case " link ": break;

case " link ": break;

I need to do this with several links. Is there anyway, using Regex, to check in a case whether or not the url has this www. and /index.html not to be creating multiple unnecessary lines? (Note: if this is the case, it can be with if / else).

    
asked by anonymous 07.03.2015 / 00:18

1 answer

1

RegExp suggestion:

/http:\/\/(www\.)?.*(index\.html)?/

To test you can use it like this:

var regex = /http:\/\/(www\.).*(index\.html)/;
if (regex.test(url)){
    // fazer algo quando tem "www." e/ou "index.html"
} else {
    // não tem
}

The regex has two optional catch groups to "catch" these strings.

But I think I'd better do this in JS with indexOf:

if (url.indexOf('www.') != -1 || url.indexOf('index.html') != -1){
    // fazer algo quando tem "www." e/ou "index.html"
} else {
    // não tem
}
    
07.03.2015 / 00:47