1.0.3 • Published 4 years ago

simple-regex-toolkit v1.0.3

Weekly downloads
6
License
ISC
Repository
github
Last release
4 years ago

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