I need a way to get the extension out of any absolute url you might have.
Out of all these things i want to see mp4 as output. I want a regEx to do it. Please help me and explain what your expression means, or point me in the riht direction.
I need a way to get the extension out of any absolute url you might have.
Out of all these things i want to see mp4 as output. I want a regEx to do it. Please help me and explain what your expression means, or point me in the riht direction.
Note that this is a dicey business. You can almost always find an input to break your pattern. However, if you're confident about the range of the inputs, something simple may suffice.
This pattern will put the extension into group 1:
\.([^/#?]+)([#?][^/]*)?$
Tests:
rex = new RegExp('\\.([^/#?]+)([#?][^/]*)?$');
rex.test('http://example.com/lol.mp4')
console.log(RegExp.$1)
// prints "mp4"
rex.test('http://example.com/lol.mp4?test')
console.log(RegExp.$1)
// prints "mp4"
rex.test('http://example.com/lol.mp4#test')
console.log(RegExp.$1)
// prints "mp4"
Explanation
\. # Match a period (note backslash is doubled in string literal)
(
[^/#?]+ # Match a positive number of non-delimiters (path, hash, or query)
) # save that to group 1
( # also,
[#?] # Match the start of an anchor or query
[^/]* # and as many non path-delimiters that follow
)? # maybe
$ # oh, and only at the end of the input
This worked like a charm.
string.split('.').pop().match(/\w+/)[0];
If there is a dot after ? or #, the following will work:
string.split(/\?|#/)[0].split('.').pop().match(/\w+/)[0];
using
/\.mp4/igm
regular expression it select all mp4 as out put