New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details
Socket
Book a DemoSign in
Socket

replace-regex-groups

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

replace-regex-groups

A lightweight library to replace the regex groups in a string by a set of replacement values

latest
npmnpm
Version
1.0.3
Version published
Weekly downloads
7
600%
Maintainers
1
Weekly downloads
 
Created
Source

replace-regex-groups

A lightweight library to replace the regex groups in a string by a set of replacement values

  • Usage
  • Input Validation
  • License

Usage

$ npm install replace-regex-groups

Indexed capture groups ( unnamed )

const replaceRegexGroups = require('replace-regex-groups')
const regex = /http:\/\/([A-Za-z0-9]*):([0-9]*)\/.*/g
const url = 'http://localhost:8080/foo/bar'
const replacements = ['127.0.0.1', 9090 ]
const replaced = replaceRegexGroups(url, regex, replacements)
// replaced is http://127.0.0.1:9090

Named capture groups

const replaceRegexGroups = require('replace-regex-groups')
const regex = /http:\/\/(?<host>[A-Za-z0-9]*):(?<port>[0-9]*)\/.*/g
const url = 'http://localhost:8080/foo/bar'
const replacements = { host: '127.0.0.1', port: 9090 }
const replaced = replaceRegexGroups(url, regex, replacements)
// replaced is http://127.0.0.1:9090

Dataset pattern replacement

const { readFileSync, writeFileSync } = require('fs')
const replaceRegexGroups = require('replace-regex-groups')
const transactions = readFileSync('./transactions.csv', 'utf8')
const sourcePattern = /(?<reference>[A-Z]{4}.[A-Z0-9]*),(?<period>[0-9]{4}.[0-9]{2}),/gm
const replacements = { reference: 'ANONYMIZED_REFERENCE', period: 'ANONYMIZED_PERIOD' }
const anonymizedTransactions = replaceRegexGroups(transactions, sourcePattern, replacements)
writeFileSync('./anonymized-transactions.csv', anonymizedTransactions)

Input validation

  • In case of unnamed capture groups the size of the replacements array must be the same as the capture groups count in the input regex
  • In case of named capture groups the keys in the replacements map must be the exact same as the capture groups names of the input regex
  • All the regex groups must be of the same kind ( either unnamed or named using ?<GROUP_NAME> syntax )
  • For a regex containing named capture groups a replacement map is expected
  • For a regex containing unnamed capture groups a replacement array is expected
  • Capture groups must not be nested

License

ISC

FAQs

Package last updated on 31 May 2021

Did you know?

Socket

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