Skip to content

Instantly share code, notes, and snippets.

@sushiljainam
Created April 18, 2017 07:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sushiljainam/88db06f006e1a47d0f511670485f3bb8 to your computer and use it in GitHub Desktop.
Save sushiljainam/88db06f006e1a47d0f511670485f3bb8 to your computer and use it in GitHub Desktop.
//regular expressions
v = /^a/.test('asdasaabb');
console.log("/^a/.test('asdasaabb')", v);
console.log("/.*/.test('/^a+b+$/')", /.*/.test('/^a+b+$/'));
console.log("/^\/.*\/$/.test('/^a+b+$/')", /^\/.*\/$/.test('/^a+b+$/'));
console.log("/^\/.\/$/.test('/^a+b+$/')", /^\/.\/$/.test('/^a+b+$/'));
console.log("/^a/.test('/^a+b+$/')", /^a/.test('/^a+b+$/'));
console.log("/^a/.test('/^a+b+$/')", /^a/.test('/^a+b+$/'));
//s = readline();
var rl = process.openStdin();
var regex = /.*/;
rl.addListener("data", function(d) {
// note: d is an object, and when converted to a string it will
// end with a linefeed. so we (rather crudely) account for that
// with toString() and then trim()
var input = d.toString().trim();
if(/^\/.+\/$/.test(input)){
regex = new RegExp(input.slice(1,(input.length-1)));
console.log('regex changed to:'+regex);
}else
{
var toTest = input;
console.log(regex+'.test("'+toTest+'") is : '+regex.test(toTest));
}
if (d.toString().trim()==='end') {
rl.destroy();
};
});
/*const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question('type a reqex (without slashes) ',
function (r) {
var regex = '/'+r+'/';
rl.close();
});
rl.question('type examples to match',
function(m){
console.log(regex.test(m));
});*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment