Skip to content

Instantly share code, notes, and snippets.

@gtzilla
Created October 26, 2011 07:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gtzilla/1315719 to your computer and use it in GitHub Desktop.
Save gtzilla/1315719 to your computer and use it in GitHub Desktop.
Regex Test, consistent fail on every other when matching - or !
var raw_str_regex = "-([a-zA-Z0-9_]+)$";
var regex = new RegExp(raw_str_regex, "gm");
var lst = [
"this is -not",
"this is -not",
"this is -not",
"this is -not",
"this is -not",
"this is -not"
];
var check_okay = [];
console.log(regex);
var test_value = regex.test( lst[0] );
console.log("test:", test_value, lst[0]);
var test_value1 = regex.test( lst[1] );
console.log("test:", test_value1, regex.source, lst[1]);
if(test_value && test_value === test_value1) {
console.log("Yey!", RegExp.lastMatch);
} else {
console.log("FAILED!!!", RegExp.lastMatch)
}
console.log(regex);
for(var i=0; i<lst.length; i++) {
//regex.test(""); fixes it??
var test_value = regex.test( lst[i] );
console.log(test_value, lst[i]);
if(test_value === true) {
check_okay.push(test_value);
}
}
if(check_okay.length === lst.length) {
console.log("okay");
} else {
console.log("FAILED!!!!!!", RegExp.lastMatch);
}
// remove the global flag, no error
var raw_str_regex = "(-[a-zA-Z0-9_]+)$";
var regex = new RegExp(raw_str_regex, "m");
var lst = [
"this is -not",
"this is -not",
"this is -not",
"this is -not",
"this is -not",
"this is -not"
];
var check_okay = [];
console.log(regex);
var test_value = regex.test( lst[0] );
console.log("test:", test_value, lst[0]);
var test_value1 = regex.test( lst[1] );
console.log("test:", test_value1, regex.source, lst[1]);
if(test_value && test_value === test_value1) {
console.log("Yey!", RegExp.lastMatch);
} else {
console.log("FAILED!!!", RegExp.lastMatch)
}
console.log(regex);
for(var i=0; i<lst.length; i++) {
//regex.test(""); fixes it??
var test_value = regex.test( lst[i] );
console.log(test_value, lst[i]);
if(test_value === true) {
check_okay.push(test_value);
}
}
if(check_okay.length === lst.length) {
console.log("okay", RegExp.lastMatch);
} else {
console.log("FAILED!!!", RegExp.lastMatch);
}
$ node regex_compile_test2.js
/-([a-zA-Z0-9_]+)$/gm
test: true this is -not
test: false -([a-zA-Z0-9_]+)$ this is -not
FAILED!!! -not
/-([a-zA-Z0-9_]+)$/gm
true 'this is -not'
false 'this is -not'
true 'this is -not'
false 'this is -not'
true 'this is -not'
false 'this is -not'
FAILED!!! "
$ node regex_compile_test2.js
/(-[a-zA-Z0-9_]+)$/m
test: true this is -not
test: true (-[a-zA-Z0-9_]+)$ this is -not
Yey! -not
/(-[a-zA-Z0-9_]+)$/m
true 'this is -not'
true 'this is -not'
true 'this is -not'
true 'this is -not'
true 'this is -not'
true 'this is -not'
okay "
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment