Socket
Socket
Sign inDemoInstall

clone-regexp

Package Overview
Dependencies
1
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    clone-regexp

Clone and modify a RegExp instance


Version published
Weekly downloads
2M
increased by6.23%
Maintainers
1
Install size
11.3 kB
Created
Weekly downloads
 

Package description

What is clone-regexp?

The clone-regexp npm package is designed to clone regular expressions with the option to modify their properties such as flags, source, and lastIndex. This can be useful when you need to reuse a regular expression with slight variations or reset its state without affecting the original regex.

What are clone-regexp's main functionalities?

Cloning a regular expression

This feature allows you to create a copy of an existing regular expression. The cloned regex will have the same source and flags as the original.

const cloneRegexp = require('clone-regexp');
const regex = /[a-z]/gi;
const clonedRegex = cloneRegexp(regex);
console.log(clonedRegex); // Output: /[a-z]/gi

Modifying flags

This feature allows you to clone a regular expression and modify its flags. In this example, the 'i' flag is removed, and the 'm' flag is added.

const cloneRegexp = require('clone-regexp');
const regex = /[a-z]/gi;
const clonedRegex = cloneRegexp(regex, {flags: 'gm'});
console.log(clonedRegex); // Output: /[a-z]/gm

Changing the lastIndex

This feature allows you to clone a regular expression and set the lastIndex property. This is useful when you want to reset the state of the regex for a new search.

const cloneRegexp = require('clone-regexp');
const regex = /[a-z]/gi;
regex.lastIndex = 10;
const clonedRegex = cloneRegexp(regex, {lastIndex: 0});
console.log(clonedRegex.lastIndex); // Output: 0

Other packages similar to clone-regexp

Readme

Source

clone-regexp

Clone and modify a RegExp instance

Install

$ npm install clone-regexp

Usage

import cloneRegexp from 'clone-regexp';

const regex = /[a-z]/gi;

cloneRegexp(regex);
//=> /[a-z]/gi

cloneRegexp(regex) === regex;
//=> false

cloneRegexp(regex, {global: false});
//=> /[a-z]/i

cloneRegexp(regex, {multiline: true});
//=> /[a-z]/gim

cloneRegexp(regex, {source: 'unicorn'});
//=> /unicorn/gi

API

cloneRegexp(regexp, options?)

regex

Type: RegExp

Regex to clone.

options

Type: object
Properties: source global ignoreCase multiline dotAll sticky unicode lastIndex

Optionally modify the cloned RegExp instance.

Keywords

FAQs

Last updated on 08 Apr 2021

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc