Skip to content

Instantly share code, notes, and snippets.

@a1exlism
Last active March 14, 2018 11:36
Show Gist options
  • Save a1exlism/a4b618456fe88c5b3977e13508a3adbb to your computer and use it in GitHub Desktop.
Save a1exlism/a4b618456fe88c5b3977e13508a3adbb to your computer and use it in GitHub Desktop.
/*
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/test
* Jack(?!Chen)
* Negative lookahead | only mach Jack OR Jack+(anything not match `Chen`)
*/
let str = 'JackBlack is not my friend.';
let tmpStr = 'JackChen is my friend.';
let withoutG= /Jack(?!Chen)/;
let withG = /Jack(?!Chen)/g;
let withG2 = /Jack(?!Chen)/g;
for(let i = 0; i < 3; i ++) {
console.log(`Normal WithoutG ${i}th ` + withoutG.test(str));
}
console.log('===================');
for(let i = 0; i < 3; i ++) {
console.log(`Normal WithG ${i}th ` + withG.test(str));
console.log(`Temp WithG ${i}th ` + withG.test(tmpStr));
console.log(`Normal WithG2 ${i}th ` + withG2.test(str));
}
// Normal WithoutG 0th true
// Normal WithoutG 1th true
// Normal WithoutG 2th true
// ===================
// Normal WithG 0th true
// Temp WithG 0th false
// Normal WithG2 0th true
// Normal WithG 1th false
// Temp WithG 1th false
// Normal WithG2 1th false
// Normal WithG 2th false
// Temp WithG 2th false
// Normal WithG2 2th true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment