views:

93

answers:

3

I need to get all instances of http://someurl/somefile.flv within a HTML file. Extension must not be case sensitive.

I currently use the following simple regular expression that I use in Javascript:

var rePattern = new RegExp(
    "(http://.*.flv)",
    "gi"
  );

It has it's flaws though, especially if there is a URL and somewhere further in the file you have something mentioning FLV.

I've found various regular expressions that check for URLs, etc but I cannot correctly modify them to just search for FLV links.

A: 

If I were doing this, my regexp would look something like:

http:\/\/(.+)\.(.+)\.flv

I escaped my special characters, as you should do. Specifically, the two slashes, and the period right before "flv". Your regexp would incorrectly match "http://123flv" which I'm sure you don't want. My version also requires a top-level-domain, e.g., example.com. Yours would have matched http://example/x.flv which clearly isn't a valid URL. Notice, also, how I use the + instead of *, to ensure that one or more characters is present.

Hope this helps.
-tjw

Travis J Webb
`http://example/x.flv` is valid.
Anurag
Chris
Chris
Chris
A: 

http:\/\/[^\"\'<>{}\s]{6,255}(?<=.flv)

Jimmy Ruska
do you mind explaining this one? As far as i can read, it matches http:// then it matches from non-url-weird chars that appear from 6 to 255 (max in url) times, then there is vortex (wtf is ?<=) then .flv. But I tested it, and if I put less than 6 chars in the main part of the URL it works as well..
MarceloRamires
@MarceloRamires (?<= is a look-behind expression to make sure the pattern ended with .flv. It's more efficient that the (.*)? style thing because it happens once the previous expression is evaluated. 'if I put less than 6 chars...' You're not counting the .flv part. Make it a bigger number.
Jimmy Ruska
I get this error:SyntaxError: Invalid regular expression: /http://[^"'<>{}s]{6,255}(?<=.flv)/: Invalid groupUsing: var rePattern = new RegExp("http:\/\/[^\"\'<>{}\s]{6,255}(?<=.flv)","gi");
Chris
+1  A: 
/http:\/\/[\S]+?\.flv/g

document.body.innerHTML.match(/http:\/\/[\S]+?\.flv/g)
S.Mark