Created
August 6, 2013 10:10
-
-
Save saturngod/6163325 to your computer and use it in GitHub Desktop.
Testing VerbalExpressions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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