@forensic-js/regex
TypeScript icon, indicating that this package has built-in type declarations

1.0.2 • Public • Published

Regex

Build Status Coverage Status semantic-release npm version npm

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

npm install @forensic-js/regex

Usage Sample

import {replace} from '@forensic-js/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

import {replaceCallback} from '@forensic-js/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

import {replace} from '@forensic-js/regex';

const text = '2222 is the amount';
console.log(
    replace(/(\d+)/, '$$:1', text)).toEqual('$2222 is the amount')
);

Package Sidebar

Install

npm i @forensic-js/regex

Weekly Downloads

0

Version

1.0.2

License

MIT

Unpacked Size

39.8 kB

Total Files

14

Last publish

Collaborators

  • harrison-ifeanyichukwu