Use Regex on switch in jQuery

Asked

Viewed 72 times

2

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

marry "http://www.link.com.br/example/": break;

marry "http://www.link.com.br/exemplo/index.html": break;

marry "http://link.com.br/example/": break;

marry "http://link.com.br/exemplo/index.html": break;

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

  • I don’t understand what you want to do... you want to detect www. in string? and do what if you have?

  • Yes, I want to do something that, whether there is or is not www. or /index.html it performs such a function. This was an example code, in fact it will perform a function in each case...

  • And you need regex for that? Can you do it with if (url.indexOf('www.') != -1) fazer algo...

  • I would like the regex simply not to have to do this, in which case I would have to do several checks. What I really wanted was "http://(if you have or don’t have www.)link.com.br/example/(if you have.

  • The url always starts with http://? The dominion is always the same?

  • Yes, exactly.

  • Forehead like this if (/http:\/\/(www\.)?.*(index\.html)?/.test("http://link.com.br/exemplo/")) fazer algo...

Show 2 more comments

1 answer

1


Suggestion from Regexp:

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

For testing 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
}

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

But I think it’s best to do this in JS with index:

if (url.indexOf('www.') != -1 || url.indexOf('index.html') != -1){
    // fazer algo quando tem "www." e/ou "index.html"
} else {
    // não tem
}
  • It didn’t work in either case, Sergio. I’m going to post the code that I was developing so you can understand what I was trying to avoid. And previously, thank you so much for offering to help.

  • This was the code: http://pastebin.com/fGChdnfp Looking, you can see my intention, that in case it was to avoid so many cases, using the regex...

  • @Are there more cases than these? Is this the whole code? Otherwise it seems to me that you only need to know when the link has /galeria/, that’s the pattern you need to define, nothing more.

  • No, and I need to know about the contact as well. Did you really understand more or less what I want with the code? Even I’m confused now :(.

  • You wouldn’t really need the /index.html. I had thought of this for security issues (prevent the function from running if it’s index.html) but, on second thought, I believe that without index.html it won’t make a difference...

  • @Igor to me you only need this: http://pastebin.com/EaaazpMs

  • Perfect, @Sergio. How did I not think of this before? Sometimes I think so much about how to do such a thing that I end up curling up and doing it the hard way. Thank you very much!

  • @Igor great! If you want you can mark the answer as accepted.

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.