Skip to content

Instantly share code, notes, and snippets.

@dioxmio
Last active August 29, 2021 14:25
Show Gist options
  • Save dioxmio/292ae5f45195b66e9ba35bc6ea73c4f4 to your computer and use it in GitHub Desktop.
Save dioxmio/292ae5f45195b66e9ba35bc6ea73c4f4 to your computer and use it in GitHub Desktop.
const citiesStartingWithB = /B.*?\w+/g
const target = "Barcelona Viena Beijing Tokio Bogota";
let result;
while ((result = citiesStartingWithB.exec(target)) !== null) {
console.log(`Match: ${result[0]}`);
console.log(`Last Index at ${citiesStartingWithB.lastIndex}`);
}
// ✅ Output
// Match: Barcelona
// Last Index at 9
// Match: Beijing
// Last Index at 23
// Match: Bogota
// Last Index at 36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment