str-seg
TypeScript icon, indicating that this package has built-in type declarations

1.0.4 • Public • Published

str-seg

Extracts segments from a string based on provided start and end markers.

Installation

npm install str-seg

Usage

Example:

import { strseg } from 'str-seg'
// let { strseg } = require('str-seg');

const string = '<p id="title">Hey there!</p><p>Hey</p><p>Hello</p>'
const segments = strseg(string, {
  title: { from: '<p id="title">', to: '</p>' },
  p: '<p>#>#</p>',
  p_all: { from: /<p\b[^>]*>/, to: '</p>' } // we can use RegEx to target all <p> tags!
})

console.log(segments.title[0]) // "Hey there!"
console.log(segments.p[1]) // "Hello"

strseg(string, segmentBy)

  • string: string - The input string from which segments will be extracted.
  • segmentBy: Record<string, string | { from: string | RegExp, to: string | RegExp, reverse: boolean }> - An object specifying the segments to extract, with keys representing segment names and values representing start and end markers. Note - 'reverse' scans using 'to' first. String can be used with '#>#' in the middle or '#<#' if in reverse.

Returns a record where keys are segment names and values are arrays of extracted segments.

License

BSD 2-Clause License

Package Sidebar

Install

npm i str-seg

Weekly Downloads

6

Version

1.0.4

License

BSD-2-Clause

Unpacked Size

26 kB

Total Files

7

Last publish

Collaborators

  • algj