Created
May 11, 2025 10:49
-
-
Save caltuntas/c09ddd9e4297a0235924f65e6f72a4f6 to your computer and use it in GitHub Desktop.
hate-regex
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const placeHolderPattern = /#{(\w+)}/g; | |
const args = process.argv.slice(2); | |
if (args.length < 2) { | |
console.log('missing arguments'); | |
process.exit(1); | |
} | |
const replacements = [ | |
{ name: 'placeholder1', value: args[0] }, | |
{ name: 'placeholder2', value: args[1] }, | |
]; | |
function replacer(match, placeHolderName) { | |
const replacement = replacements.find(r => r.name === placeHolderName); | |
if (!replacement) { | |
throw new Error(`Replacement for ${placeHolder} place holder does not exist.`); | |
} | |
return replacement.value || ''; | |
} | |
let output = 'some text template #{placeholder1} and #{placeholder2} and some more text #{placeholder1}'; | |
output = output.replace(placeHolderPattern, replacer); | |
console.log(output); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const args = process.argv.slice(2); | |
if (args.length < 2) { | |
console.log('missing arguments'); | |
process.exit(1); | |
} | |
const replacements = [ | |
{ name: 'placeholder1', value: args[0] }, | |
{ name: 'placeholder2', value: args[1] }, | |
]; | |
let output = 'some text template #{placeholder1} and #{placeholder2} and some more text #{placeholder1}'; | |
replacements.forEach((item) => { | |
output = output.replaceAll(`#{${item.name}}`, item.value || ''); | |
} | |
); | |
console.log(output); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const args = process.argv.slice(2); | |
if (args.length < 2) { | |
console.log('missing arguments'); | |
process.exit(1); | |
} | |
const replacements = [ | |
{ name: 'placeholder1', value: args[0] }, | |
{ name: 'placeholder2', value: args[1] }, | |
]; | |
let output = 'some text template #{placeholder1} and #{placeholder2} and some more text #{placeholder1}'; | |
replacements.forEach((item) => { | |
const placeholder = `#{${item.name}}`; | |
while (output.includes(placeholder)) { | |
output = output.replace(placeholder, ()=>item.value || ''); | |
} | |
} | |
); | |
console.log(output); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const placeHolderPattern = /#{(\w+)}/; | |
const allPlaceHoldersPattern = new RegExp(placeHolderPattern, 'g'); | |
const args = process.argv.slice(2); | |
if (args.length < 2) { | |
console.log('missing arguments'); | |
process.exit(1); | |
} | |
const replacements = [ | |
{ | |
name: 'placeholder1', | |
value: args[0] | |
}, | |
{ | |
name: 'placeholder2', | |
value: args[1] | |
}, | |
]; | |
let output = 'some text template #{placeholder1} and #{placeholder2} and some more text #{placeholder1}'; | |
let matches; | |
do { | |
console.log('after replacement output=' + output); | |
matches = allPlaceHoldersPattern.exec(output); | |
if (matches) { | |
const placeHolder = matches[1]; | |
const replacement = replacements.find(r => r.name === placeHolder); | |
if (!replacement) { | |
throw new Error(`Replacement for ${placeHolder} place holder does not exist.`); | |
} | |
output = output.replace(placeHolderPattern, replacement.value || ''); | |
allPlaceHoldersPattern.lastIndex = 0; | |
} | |
} while (matches); | |
console.log(output); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment