Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/pionl/match-replace-all
Finds strings by given match and replaces the content using callback function
https://github.com/pionl/match-replace-all
javascript regular-expression replace-text
Last synced: 25 days ago
JSON representation
Finds strings by given match and replaces the content using callback function
- Host: GitHub
- URL: https://github.com/pionl/match-replace-all
- Owner: pionl
- Created: 2017-09-26T13:57:39.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2017-09-26T14:10:30.000Z (over 7 years ago)
- Last Synced: 2024-12-13T04:39:00.242Z (about 2 months ago)
- Topics: javascript, regular-expression, replace-text
- Language: JavaScript
- Size: 37.1 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
# Match all occurrences in string and replace it
Finds all occurrences in the string for given regex and replaces the value with a new string (using callback function).## Install
```bash
npm install match-replace-all
```## Usage
- Create a new RegExp object with `global` and `multiline` option.
- Provide a callback that will receive match array and return a new string that will replace the **original string**.
- Callback can return `false` to skip replacement.
- If the new value is same as original, no replacement is done.```javascript
import matchReplaceAll from 'match-replace-all'const regex = new RegExp(']*)>([^<]+)', 'gm')
const string = 'testing first second'
// Add href attribute to link with the links value
const newString = matchReplaceAll(regex, string, (match) => {
const value = match[2]
const attributes = match[1]
return `${value}`
})
console.log(newString)
```