Skip to content

Instantly share code, notes, and snippets.

@crutchcorn
Created November 28, 2023 08:36
Show Gist options
  • Save crutchcorn/c89b010ed794ec17333d5a80d038baca to your computer and use it in GitHub Desktop.
Save crutchcorn/c89b010ed794ec17333d5a80d038baca to your computer and use it in GitHub Desktop.
A method to generate ignored indexes and do partial replacement in a regex
// Given an input
const input = "header 123 {#custom-id}"
// Provide a tranformation of said input that keeps the same length
// IE: "Capitalizing" a title in a markdown file
const transformedInput = input.toUpperCase();
// However, we don't want to transform this regex
// IE: A custom ID
const ignored = ["{#custom-id}"]
// From this, our output should be:
// "HEADER 123 {#custom-id}"
// Below is the implementation
let ignoredIndexes = new Set();
for (let regex of ignored) {
const matches = input.match(new RegExp(regex, 'gmu'));
if (!matches) continue;
let lastIndex = 0;
for (const match of matches) {
const matchedIndex = input.indexOf(match, lastIndex);
lastIndex = matchedIndex + match.length;
for (let indexOfMatchLength = 0; indexOfMatchLength < match.length; indexOfMatchLength++) {
ignoredIndexes.add(matchedIndex + indexOfMatchLength);
}
}
}
let output = "";
for (let inputIndex = 0; inputIndex < input.length; inputIndex++) {
if (ignoredIndexes.has(inputIndex)) {
output += input[inputIndex];
} else {
output += transformedInput[inputIndex];
}
}
// Finally, output the result:
console.log(output);
@crutchcorn
Copy link
Author

@tobySolutions That's intentional behavior😊

We don't want to escape the regexes, we want to capture them as-written

@tobySolutions
Copy link

Thank you very much!! @crutchcorn

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment