Capture text fragments based on regular expressions that are executed for each line of the text.
npm install @uppercod/str-fragment
import { getFragments, replaceFragments } from "@uppercod/str-fragment";
let str = `
/**
* old comment
*/
let s = "";
`;
/**@type {import("str-fragment/internal").captures}*/
let fragments = getFragment(str, {
open: /\/\*\*/,
end: /\*\//,
equal: false, // if fragments share capture equality set equal to true
});
replaceFragments(str, fragments, () => `/** new comment */`);
// /** new comment */
// let s = "";
template-string
let blocks = getFragments(code, {
open: /css`/,
end: /`/,
});
frontmatter
let blocks = getFragments(code, {
open: /^---/m,
end: /^---/m,
equal: true,
});
Replace the fragments with a new text, if null returns no replacement of the evaluated fragment is generated
Walk on the shards generate changes