Note that there are some explanatory texts on larger screens.

plurals
  1. PORegular expression to catch as many Javascript redirections as possible
    primarykey
    data
    text
    <p>I'm trying to catch as many Javascript redirects as possible from many HTML pages. My regular expression is:</p> <pre><code>((location.href)|(window.location)|(location.replace)|(location.assign))(( ?= ?)|( ?\( ?))("|')([^'"]*)("|')( ?\) ?)?; </code></pre> <p>I use Python but the question is general:</p> <pre><code>regex = re.compile(r"""((location.href)|(window.location)|(location.replace)|(location.assign))(( ?= ?)|( ?\( ?))("|')([^'"]*)("|')( ?\) ?)?;""", re.I) # ... some control here ... print re.search(regex, html).group(10) # 10 is the pure url </code></pre> <p>I did some tests and I was able to catch all these cases.</p> <pre><code>location.href = "http://www.foo.com"; location.href="http://www.foo.com"; window.location = "http://www.foo.com"; window.location.href = "http://www.foo.com"; location.replace ("http://www.foo.com"); location.replace( "http://www.foo.com" ) ; location.assign ("http://www.foo.com"); </code></pre> <p>And skip where I can't resolve an URL because the code contains a variable:</p> <pre><code>location.href = "http://www.foo.com" + var + "something else"; </code></pre> <p>The questions are:</p> <ol> <li>Are there other ways to redirect using Javascript? Other <code>location.somethingelse</code> that I am missing?</li> <li>Is the way I catch these 4 cases correct? Is it allowed to have something like <code>location.href = http://www.foo.com;</code> or <code>location.replace (http://www.foo.com);</code> that I'll miss because of the (double) quotes? Am I too strict or too lax?</li> <li>Is my regex well written? Or can I improve it in some way?</li> </ol>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload