An open API service indexing awesome lists of open source software.

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

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