@uppercod/str-fragment
v1.3.3
Published
Capture text fragments based on regular expressions that are executed for each line of the text
Downloads
22
Readme
str-replace
Capture text fragments based on regular expressions that are executed for each line of the text.
Install
npm install @uppercod/str-fragment
Example
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 = "";
more expressions
template-string
let blocks = getFragments(code, {
open: /css`/,
end: /`/,
});
frontmatter
let blocks = getFragments(code, {
open: /^---/m,
end: /^---/m,
equal: true,
});
Api
replaceFragments
Replace the fragments with a new text, if null returns no replacement of the evaluated fragment is generated
walkFragments
Walk on the shards generate changes