views:

68

answers:

6

I am wondering what this technique is called and what it does. It seems to be validating some regular expression on the variable url. I am customizing another persons code:

var url = document.getElementById("editorURL").value;   
if(/(file|http).*/.test(url)) {

}

Maybe someone has a link to an article that explains this a bit more in-depth?

+2  A: 

Essentially: if (url matches some_expression) { /* do something * }

http://www.w3schools.com/jsref/jsref_obj_regexp.asp

I do understand if statements but the question concerned the expression and the test() function. Thanks anyway.
WmasterJ
Hence the link explaining the regexp object, its syntax and methods.
Took away my -1
WmasterJ
+2  A: 

This is a regular expression checking whether url begins with either file or http, an effort to make sure the value entered is a proper URL pointing to a resource that can be opened in a browser.

Seeing as it fails to check for the subsequent :// part (and would for example match filexyz or httphttphttp), I would say it is not perfect.

More on the test() method at W3Schools.

The test() method tests for a match in a string.

This method returns true if it finds a match, otherwise it returns false.

Pekka
Thanks for the answer and recommendation on improving it further :D
WmasterJ
+1  A: 

I find regular-expressions.info to be the best all-around reference for regular expressions. Their tutorial goes all the way from simple to complex and they also include info about what works in the various flavours of regular expression engine.

Daniel Vandersluis
Thanks for the URL tip!
WmasterJ
+2  A: 
/(file|http).*/ 

This is a regular expression that matches any string beginning with file or http, so in this case, any URL using those protocols.

regex.test(string)

This is a function that tests if the regex matches the string and returns a boolean, so together they make is a very basic url validation method.

Paul Creasey
+3  A: 

It's called "validating" (!), and it's meant to check that "editorURL" contains a URL containing the word "file" or "http". It's not that good of a regex actually, but better than nothing. A start would be to at least check that file or http are at the beginning of the URL, not as it is now anywhere within it.

/^(file|http).*/.test(url)

This is also only client-side validation, meant to give the user instant feedback on the validation. You should have at least the same rules, possible even stricter, on the server-side as well. The server-side validation are for catching errors before they reach your database/data-layer, but also to protect against crackers (anyone can work around the client-side validation if they want to, so never trust data from the client).

Edit: My pattern above means (step by step):

  • / this is the beginning of the regex pattern
  • ^ start matching from the beginning of the text (beginning of the url variable. This is missing in original pattern)
  • ( start a sub-pattern (pattern within the pattern - mostly meant for grouping)
  • file|http match literally file OR http
  • ) end of sub-pattern
  • . any character
  • * repeat the last character zero or more times (in this case "any character(s)")
  • / end of regex pattern
Emil Vikström
Thank you for a comprehensive breakdown of the regexp.
WmasterJ
A: 

It's an attempt to check whether the url starts with file:// or http://. It's based on an incorrect assumption, though. The author assumes that the test() method only returns true if it matches the whole string, but it returns true if it matches any part of the string. For example, this evaluates to true:

/(file|http).*/.test(' a test file')

You could get rid of the .* part and it would still get the same results for any given string.

Since the person who wrote it got this wrong, there's probably other things that are wrong, too. I suggest rewriting bits an pieces, after dissecting their functionality. It will be much less frustrating than making tiny changes to buggy code. Good luck!

Ben Atkin