JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 10
  • Score
    100M100P100Q34993F
  • License ISC

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

Package Exports

  • als-replace-between
  • als-replace-between/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (als-replace-between) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

als-replace-between

als-replace-between is a JavaScript library specifically designed for advanced text manipulation, enabling the handling of nested structures and complex content patterns. This utility excels in scenarios where traditional string replacement tools fall short, such as when dealing with nested tags or layered data structures.

It is designed to work both in Node.js and browser environments.

Key Features

  • Tree Construction: Constructs a hierarchical tree based on provided start and end markers, capturing nested and overlapping segments.
  • Content Replacement: Utilizes the constructed tree to perform precise and context-aware replacements within the text.

Installation

npm install als-replace-between

Concept

The library operates in two main phases:

  1. Tree Construction: The buildTree method parses the input text to construct a tree structure that reflects the nesting and scope of the specified markers. This tree is essential for understanding the context and relationships within the text.
  2. Content Replacement: Once the tree is built, the replace method allows for dynamic modification of the text based on the tree structure. This method supports multiple modifiers, each capable of transforming the text in a context-specific manner.

Tree Structure

Each node in the tree represents a segment of the original text bounded by the start and end markers. Nodes can contain child nodes, representing nested structures. Here's what each node in the tree contains:

  • content: The text within the current segment.
  • children: An array of child nodes, representing nested segments.
  • start, end: The positions in the original text where this segment begins and ends.
  • parent: The parent node if it's a child

Example of Tree Structure

Given the text "Hello worldinside End", the tree would look like this:

  • Node: "Hello worldinside End"
    • Child Node: "worldinside"
      • Child Node: "inside"

Usage

const content = "Hello <tag>world<tag>inside</tag></tag>!";
const startR = /<tag>/
const endR = /<\/tag>/
const instance = new ReplaceBetween(content, startR, endR);
// now available instance.tree

const modifiers = [
  (text, node) => text.toUpperCase(), // Modifies text to uppercase
  (text, node) => `Modified: ${text}` // Adds a prefix to the text
]
const result = instance.replace(modifiers)

There is static ReplaceBetween.replaceBetween(content, startR, endR, modifiers, context) method, that does building tree and replacement and returns the result.

Parameters:

  • content (String): The content within which text will be replaced.
  • startR (RegExp): A regular expression defining the start of the replaceable area.
  • endR (RegExp): A regular expression defining the end of the replaceable area.
  • modifiers (Array of Functions): Functions to modify the content between the start and end markers. Each function should take the text segment as an argument and return the modified text.