// source: https://swizec.com/blog/finally-a-practical-use-case-for-javascript-generators/swizec/9036
const UserRegex = new RegExp(/@(\w+)/, "g");
function* regexGenerator(string, regex) {
let match = null;
do {
match = regex.exec(string);
if (match) {
yield match;
}
} while (match);
}
const string = "this is a test with @swizec and @kyleshevlin, maybe @lukeed05";
const getUsernames = regexGenerator(string, UserRegex);
for (const username of getUsernames) {
console.log(username)
// ["@swizec", "swizec"]
// ["@kyleshevlin", "kyleshevlin"]
// ["@lukeed05", "lukeed05"]
}
console.log(Array.isArray(string.matchAll(UserRegex)))
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.