Skip to content

A simple library that help you escape string for regex and form regex from string. And add regex search method for object key and array index.

License

Notifications You must be signed in to change notification settings

scrwdrv/simple-regex-toolkit

Repository files navigation

simple-regex-toolkit

A simple library that help you escape string for regex and form regex from string. And add regex search method for object key and array index.

Installation

npm i simple-regex-toolkit

Usage

Esacpe & Build Regex

import * as regex from 'simple-regex-toolkit';

const string = '@%#dh.v/]dsf2\\[]&^DSD',
    reg = regex.from(regex.escape(string)),
    reg2 = regex.from(`/${regex.escape(string)}/gi`);

console.log(reg.test(string));
// true

console.log(string.toLocaleLowerCase().repeat(2).match(reg2));
// [ '@%#dh.v/]dsf2\\[]&^dsd', '@%#dh.v/]dsf2\\[]&^dsd' ]

Search Methods

const array = ['example', 'string', 'and', 'number', Date.now()];
console.log(array.indexOfRegex(/^\d+$/));
// 4

const object = {
    id: 'P0293717623',
    price: 1000,
    unit: 'usd',
    weight: 500
}
console.log(object.keyOfRegex(/^p/));
// price

Validate Regex

regex.isRegex('/google/i');
// true

regex.isRegex('google;);
// false

About

A simple library that help you escape string for regex and form regex from string. And add regex search method for object key and array index.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published