3

I'm trying to create a small script that detects whether the string input is either:

1) a URL (which will hold a filename): 'http://ajax.googleapis.com/html5shiv.js'

2) just a filename: 'html5shiv.js'

So far I've found this but I think it just checks the URL and file extension. Is there an easy way to make it so it uses an 'or' check? I'm not very experienced with RegExp.

var myRegExp = /[^\\]*\.(\w+)$/i;

Thank you in advance.

Andy G
  • 19,232
  • 5
  • 47
  • 69
Stephen Jenkins
  • 1,776
  • 3
  • 24
  • 39

8 Answers8

2

How bout this regex?

(\.js)$

it checks the end of the line if it has a .js on it.

$ denotes end of line.

tested here.

progrenhard
  • 2,333
  • 2
  • 14
  • 14
1

Basically, to use 'OR' in regex, simply use the 'pipe' delimiter.

(aaa|bbb)

will match

aaa

or

bbb

For regex to match a url, I'd suggest the following:

\w+://[\w\._~:/?#\[\]@!$&'()*+,;=%]*

This is based on the allowed character set for a url.

For the file, what's your definition of a filename?

If you want to search for strings, that match "(at least) one to many non-fullstop characters, followed by a fullstop, followed by (at least) one to many non-fullstop characters", I'd suggest the following regex:

[^\.]+\.[^\.]+

And altogether:

(\w+://[\w\._~:/?#\[\]@!$&'()*+,;=%]*|[^\.]+\.[^\.]+)

Here's an example of working (in javascript): jsfiddle

You can test it out regex online here: http://gskinner.com/RegExr/

Community
  • 1
  • 1
Nick Grealy
  • 24,216
  • 9
  • 104
  • 119
  • Why is there no `%` in the URL regex? – Rudie Aug 29 '13 at 22:38
  • Great question! The answer is I was lazy and just ripped the valid characters straight from this answer (should've done my homework properly) - http://stackoverflow.com/questions/7109143/what-characters-are-valid-in-a-url#7109208 ! I've updated the answer to include '%'. – Nick Grealy Aug 29 '13 at 22:50
1

If it is for the purpose of flow control you can do the following:

var test = "http://ajax.googleapis.com/html5shiv.js";    

// to recognize http & https
var regex = /^https?:\/\/.*/i;    
var result = regex.exec(test);    
if (result == null){
    // no URL found code
} else {
    // URL found code
}

For the purpose of capturing the file name you could use:

var test = "http://ajax.googleapis.com/html5shiv.js";    

var regex = /(\w+\.\w+)$/i;    
var filename = regex.exec(test);    
Jesko R.
  • 827
  • 10
  • 21
0

Have a look at this (requires no regex at all):

var filename = string.indexOf('/') == -1
             ? string
             : string.split('/').slice(-1)[0];
RienNeVaPlu͢s
  • 7,442
  • 6
  • 43
  • 77
0

Yes, you can use the alternation operator |. Be careful, though, because its priority is very low. Lower than sequencing. You will need to write things like /(cat)|(dog)/.

It's very hard to understand what you exactly want with so few use/test cases, but

(http://[a-zA-Z0-9\./]+)|([a-zA-Z0-9\.]+)

should give you a starting point.

Mario Rossi
  • 7,651
  • 27
  • 37
0

If it's a URL, strip it down to the last part and treat it the same way as "just a filename".

function isFile(fileOrUrl) {
    // This will return everything after the last '/'; if there's
    // no forward slash in the string, the unmodified string is used
    var filename = fileOrUrl.split('/').pop();
    return (/.+\..+/).test(filename);
}
Trevor Dixon
  • 23,216
  • 12
  • 72
  • 109
0

Try this:

var ajx = 'http://ajax.googleapis.com/html5shiv.js';
function isURL(str){
  return /((\/\w+)|(^\w+))\.\w{2,}$/.test(str);
}
console.log(isURL(ajx));
StackSlave
  • 10,613
  • 2
  • 18
  • 35
0

Here is the program!

<script>
var url="Home/this/example/file.js";
var condition=0;
var result="";
for(var i=url.length; i>0 && condition<2 ;i--)
{
    if(url[i]!="/" && url[i]!="."){result= (condition==1)? (url[i]+result):(result);}
    else{condition++;}
}
document.write(result);
</script>
WearFox
  • 293
  • 2
  • 19