https://github.com/teclone/regex
A module that builds on the existing RegExp module, making it easier working with text matching and replacement both in the browser and node environments
https://github.com/teclone/regex
placeholder-replacement regex replace replace-callback replace-text string-matching
Last synced: 10 months ago
JSON representation
A module that builds on the existing RegExp module, making it easier working with text matching and replacement both in the browser and node environments
- Host: GitHub
- URL: https://github.com/teclone/regex
- Owner: teclone
- Created: 2019-04-07T14:20:46.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2022-12-08T21:31:51.000Z (over 3 years ago)
- Last Synced: 2025-06-11T14:44:53.792Z (10 months ago)
- Topics: placeholder-replacement, regex, replace, replace-callback, replace-text, string-matching
- Language: TypeScript
- Homepage:
- Size: 3.58 MB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 20
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Regex
[](https://travis-ci.org/teclone/regex)
[](https://coveralls.io/github/teclone/regex?branch=master)
[](https://github.com/semantic-release/semantic-release)
[](https://badge.fury.io/js/%40teclone%2Fregex)

Regex is a module that builds on the existing `RegExp` module, making it easier working with text matching and replacment in JavaScript both in the `browser` and in `Node.JS` environment.
**Single Browser distributable bundle is located inside **dist** folder.**
## Installation
```bash
npm install @teclone/regex
```
## Usage Sample
```typescript
import { replace } from '@teclone/regex';
let text = 'Is is Is is Is';
console.log(replace('is', 'are', text)); // are are are are are
// respect case (case sensitive)
console.log(replace('is', 'are', text, true)); // Is are Is are Is
// replace only first occurence, case sensitive false
console.log(replace('is', 'are', text, false, 1)); // are is Is is Is
// replace only first 2 occurences, case sensitive false
console.log(replace('is', 'are', text, false, 2)); // are are Is is Is
```
## Replacing with a Callback method
```typescript
import { replaceCallback } from '@teclone/regex';
const text = 'He loves her';
console.log(
replaceCallback(
['he', 'she'],
function(matches, count) {
if (matches[0].toLowerCase() === 'he') return 'She';
else {
return 'him';
}
},
text,
),
); // logs She loves him
```
## Referencing captured groups in replacement text
to reference a captured parameter in replacement text, use the format `$:number`. e.g **\$:1**, **\$:2**, **\$:3**
```typescript
import {replace} from '@teclone/regex';
const text = '2222 is the amount';
console.log(
replace(/(\d+)/, '$$:1', text)).toEqual('$2222 is the amount')
);
```