Javscript - VerbalExpression to Construct RegExr

// https://github.com/VerbalExpressions/JSVerbalExpressions var tester = VerEx() .startOfLine() .then( "http" ) .maybe( "s" ) .then( "://" ) .maybe( "www." ) .anythingBut( " " ) .endOfLine(); var test1 = "https://www.google.com"; var test2 = "ftp://google.com"; var r = document.getElementById('results'); function write(x){r.innerHTML += "<li>"+x+"</li>"} write("regEx: " + tester); write("Testing: " + test1); write(tester.test(test1) ? "Result: Good URL" : "Result: Bad URL"); write("Testing: " + test2); write(tester.test(test2) ? "Result: Good URL" : "Result: Bad URL"); /* HTML <div> <h2><a target='_blank' href='http://www.javascriptoo.com/verbalexpressions'>VerbalExpressions</a><br><h5>a JavaScript library that helps to construct hard regular expressions.</h5></h2> </div> <ul id="results"></ul> */ /* OUTPUT regEx: /^(http)(s)?(\:\/\/)(www\.)?([^\ ]*)$/gm Testing: https://www.google.com Result: Good URL Testing: ftp://google.com Result: Bad URL */
http://www.javascriptoo.com/verbalexpressions - a JavaScript library that helps to construct hard regular expressions. This ends up being a good way to start a structured RegEx but ultimately they need cleaned up depending on your goals. https://regex101.com and http://regexr.com are good testers. Things like `Range` are good but I haven't found a way to add a `+` next to it for matches beyond a single character.

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.