Skip to content

Instantly share code, notes, and snippets.

@saturngod
Created August 6, 2013 10:10
Show Gist options
  • Save saturngod/6163325 to your computer and use it in GitHub Desktop.
Save saturngod/6163325 to your computer and use it in GitHub Desktop.
Testing VerbalExpressions
var tester = VerEx()
.then( "http" )
.maybe( "s" )
.then( "://" )
.maybe( "www." )
.anythingBut( " " );
// Create an example URL
var testMe = "This is testing. https://www.google.com is a URL. http://www.facebook.com also URL.";
var result = testMe.match(tester);
console.log(result);
console.log( tester );
// Create an example of how to test for correctly formed URLs
var tester = VerEx()
.then( "http" )
.maybe( "s" )
.then( "://" )
.maybe( "www." )
.anythingBut( " " );
// Create an example URL
var testMe = "This is testing. https://www.google.com is a URL. http://www.facebook.com also URL.";
var result = testMe.replace(tester,"**URL**");
console.log(result);
console.log( tester );
// Create an example of how to test for correctly formed URLs
var tester = VerEx()
.startOfLine()
.then( "http" )
.maybe( "s" )
.then( "://" )
.maybe( "www." )
.anythingBut( " " )
.endOfLine();
// Create an example URL
var testMe = "https://www.google.com";
// Use RegExp object's native test() function
if( tester.test( testMe ) ) alert( "We have a correct URL "); // This output will fire
else alert( "The URL is incorrect" );
console.log( tester ); // Ouputs the actual expression used: /^(http)(s)?(\:\/\/)(www\.)?([^\ ]*)$/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment