Skip to content

Instantly share code, notes, and snippets.

@rauschma rauschma/README.md
Last active Dec 31, 2019

Embed
What would you like to do?

Automatically generated result table

const s='##-#';

const r=/#/g; r.lastIndex=1;
r.exec(s)             .index=1       .lastIndex updated  
r.test(s)             true           .lastIndex updated  
s.replace(r, 'x')     "xx-x"         .lastIndex reset    
s.replaceAll(r, 'x')  "xx-x"         .lastIndex reset    
s.match(r)            ["#","#","#"]  .lastIndex reset    
s.matchAll(r)         [["#"],["#"]]  .lastIndex unchanged

const r=/#/y; r.lastIndex=1;
r.exec(s)             .index=1   .lastIndex updated  
r.test(s)             true       .lastIndex updated  
s.replace(r, 'x')     "#x-#"     .lastIndex updated  
s.replaceAll(r, 'x')  TypeError
s.match(r)            .index=1   .lastIndex updated  
s.matchAll(r)         TypeError

const r=/#/yg; r.lastIndex=1;
r.exec(s)             .index=1   .lastIndex updated  
r.test(s)             true       .lastIndex updated  
s.replace(r, 'x')     "xx-#"     .lastIndex reset    
s.replaceAll(r, 'x')  "xx-#"     .lastIndex reset    
s.match(r)            ["#","#"]  .lastIndex reset    
s.matchAll(r)         [["#"]]    .lastIndex unchanged

(Older versions of .matchAll() don’t throw a TypeError if /g is not set.)

require('string.prototype.replaceall').shim();
function printUsage(flags, name, callback) {
let output = [];
output.push(name)
const re = new RegExp('#', flags);
re.lastIndex = 1;
try {
const result = callback(re, '##-#', 'x');
if (result && typeof result === 'object' && 'index' in result) {
output.push('.index='+result.index);
} else if (['number', 'boolean', 'string'].includes(typeof result) || Array.isArray(result)) {
output.push(JSON.stringify(result));
} else {
output.push('');
}
if (re.lastIndex === 0) {
output.push('.lastIndex reset');
} else if (re.lastIndex === 1) {
output.push('.lastIndex unchanged');
} else {
output.push('.lastIndex updated');
}
} catch (err) {
output.push(err.constructor.name);
output.push('');
}
return output;
}
function extractColumn(table, colIndex) {
return table.map(row => row[colIndex]);
}
function printTable(table) {
const columnWidths = [...table[0].keys()].map(
(i) => Math.max(...extractColumn(table, i).map(str => str.length)));
for (const row of table) {
const rowStr = row
.map((cell, colIndex) => cell.padEnd(columnWidths[colIndex]))
.join(' ');
console.log(rowStr);
}
}
console.log("const s='##-#';");
console.log();
for (const flags of ['g', 'y', 'yg']) {
console.log(`const r=/#/${flags}; r.lastIndex=1;`);
printTable([
printUsage(flags, 'r.exec(s)',
(re, str) => re.exec(str)),
printUsage(flags, 'r.test(s)',
(re, str) => re.test(str)),
printUsage(flags, "s.replace(r, 'x')",
(re, str, replacement) => str.replace(re, replacement)),
printUsage(flags, "s.replaceAll(r, 'x')",
(re, str, replacement) => str.replaceAll(re, replacement)),
printUsage(flags, 's.match(r)',
(re, str) => str.match(re)),
printUsage(flags, 's.matchAll(r)',
(re, str) => [...str.matchAll(re)]),
]);
console.log();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.