https://github.com/uppercod/str-fragment
Capture text blocks based on regular expressions
https://github.com/uppercod/str-fragment
Last synced: 4 months ago
JSON representation
Capture text blocks based on regular expressions
- Host: GitHub
- URL: https://github.com/uppercod/str-fragment
- Owner: UpperCod
- Created: 2020-07-11T03:32:15.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2024-09-27T06:34:18.000Z (over 1 year ago)
- Last Synced: 2025-07-01T04:45:25.817Z (12 months ago)
- Language: JavaScript
- Size: 128 KB
- Stars: 2
- Watchers: 0
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# str-replace
Capture text fragments based on regular expressions that are executed for each line of the text.
## Install
```bash
npm install @uppercod/str-fragment
```
## Example
```js
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**
```js
let blocks = getFragments(code, {
open: /css`/,
end: /`/,
});
```
**frontmatter**
```js
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