New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

als-replace-between

Package Overview
Dependencies
Maintainers
0
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

als-replace-between

Replace text between matched start and end points defined by regular expressions

  • 1.0.0
  • npm
  • Socket score

Version published
Weekly downloads
0
decreased by-100%
Maintainers
0
Weekly downloads
 
Created
Source

als-replace-between

The als-replace-between library provides a utility for replacing text between matched start and end points defined by regular expressions. It is designed to work both in Node.js and browser environments.

Installation

npm install als-replace-between

Usage

Quick Replacement

For a quick text replacement within specified boundaries:

const {replaceBetween} = require('als-replace-between');

const content = "Hello <start>world<end>!";
const modifiedContent = replaceBetween(content, /<start>/, /<end>/, [t => t.toUpperCase()]);

console.log(modifiedContent); // Outputs: Hello <START>WORLD<END>!
const content = "Hello <start>world, <start>again<end><end>!";
const modifiers = [
   t => t.replace(/>(.*?)</,(m,t) => m.replace(t,t.toUpperCase()))
]   
const replacer = new ReplaceBetween(content, /<start>/, /<end>/, modifiers);

console.log(replacer.result); // Outputs: Hello <start>WORLD, <start>AGAIN<end><end>!

Parameters

  • content: The string within which text replacements are to be made. Must be a string.
  • startR and endR: Regular expressions defining the start and end of the replacement boundaries.
  • modifiers: An array of functions that each take a string segment and return a modified string. These functions can perform any operation, including further calls to ReplaceBetween.
  • context (optional): An object that provides additional data accessible to modifiers.

Error Handling

Errors are thrown in the following scenarios:

  • If content is not a string.
  • If either startR or endR are not instances of RegExp.
  • If modifiers is not an array.
  • If there are unmatched start and end tags in the content.

Advanced usage

The folowing example will collect properties and will replace all {} to ${}.

const content = /*html*/`<div some={var}>{arr.map(item => item)}</div>`;
const modifiers = [
   (text,context,parent) => {
      if(parent.match(new RegExp(`\w*=${text}`))) context.props.push(text.slice(1,-1))
      return '$'+text
   }
];
const context = {props:[]}
const replacer = new ReplaceBetween(content, /\{/, /\}/, modifiers,context);

console.log(replacer.result); // <div some=${var}>${arr.map(item => item)}</div>
console.log(context) // {"props": ["var"]}

FAQs

Package last updated on 28 Jun 2024

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

SocketSocket SOC 2 Logo

Product

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

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc