Posts / stringToRegExp#7

String to RegExp

By Toby Hinloopen at 2021-03-18 11:57

Installation

# npm
npm i https://boomm.io/p/7-string-to-reg-exp.tar.gz
# yarn
yarn add https://boomm.io/p/7-string-to-reg-exp.tar.gz

Usage

const stringToRegExp = require("7-string-to-reg-exp");

Source

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.stringToRegExp = void 0;
function stringToRegExp(str) {
    return new RegExp(`^${str.replace(/[|\\{}()[\]^$+*?.]/g, "\\$&")}$`);
}
exports.stringToRegExp = stringToRegExp;