CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-sugar

A comprehensive JavaScript utility library for working with native objects that extends Array, Date, Function, Number, Object, RegExp, and String with powerful methods.

Pending
Overview
Eval results
Files

regexp.mddocs/

RegExp Module

Regular expression utilities for escaping strings and manipulating regex flags.

Core Imports

// Import Sugar namespace
import Sugar from "sugar";
// Methods available as Sugar.RegExp.methodName()

CommonJS:

const Sugar = require("sugar");
// Methods available as Sugar.RegExp.methodName()
Sugar.RegExp.escape('text');

Capabilities

RegExp Utility

Utility functions for working with regular expressions.

Escape

Escapes special regex characters in a string to create a literal match pattern.

/**
 * Escapes special regex characters in string for literal matching
 * @param str - String to escape for regex use
 * @returns Escaped string safe for regex patterns
 */
function escape(str?: string): string;

Usage Example:

import Sugar from "sugar";

const userInput = "What is $100 + $200?";
const escaped = Sugar.RegExp.escape(userInput);
console.log(escaped); // "What is \\$100 \\+ \\$200\\?"

// Use in regex for literal matching
const regex = new RegExp(escaped);
const text = "The question was: What is $100 + $200?";
console.log(regex.test(text)); // true

// Without escaping (would fail)
const badRegex = new RegExp(userInput);
// This would create invalid regex due to special characters

RegExp Manipulation

Methods for getting and setting regular expression flags.

Get Flags

Gets the flags from a regular expression as a string.

/**
 * Gets flags from regular expression
 * @param instance - RegExp to get flags from
 * @returns String containing all flags (e.g., "gim")
 */
function getFlags(instance: RegExp): string;

Usage Example:

import Sugar from "sugar";

const regex1 = /pattern/gi;
console.log(Sugar.RegExp.getFlags(regex1)); // "gi"

const regex2 = /test/m;
console.log(Sugar.RegExp.getFlags(regex2)); // "m"

const regex3 = /simple/;
console.log(Sugar.RegExp.getFlags(regex3)); // ""

Set Flags

Creates a new regular expression with the specified flags, replacing existing flags.

/**
 * Creates new regex with specified flags (replaces existing)
 * @param instance - RegExp to modify
 * @param flags - New flags to set (e.g., "gi")
 * @returns New RegExp with specified flags
 */
function setFlags(instance: RegExp, flags: string): RegExp;

Usage Example:

import Sugar from "sugar";

const original = /pattern/i;
const modified = Sugar.RegExp.setFlags(original, "gm");

console.log(original.flags); // "i"
console.log(modified.flags); // "gm"
console.log(original.source); // "pattern"
console.log(modified.source); // "pattern"

// Completely replace flags
const regex = /test/gim;
const newRegex = Sugar.RegExp.setFlags(regex, "s");
console.log(newRegex.flags); // "s" (all previous flags removed)

Add Flags

Creates a new regular expression by adding flags to existing ones.

/**
 * Creates new regex by adding flags to existing ones
 * @param instance - RegExp to modify
 * @param flags - Flags to add (e.g., "g")
 * @returns New RegExp with additional flags
 */
function addFlags(instance: RegExp, flags: string): RegExp;

Usage Example:

import Sugar from "sugar";

const original = /pattern/i;
const withGlobal = Sugar.RegExp.addFlags(original, "g");

console.log(original.flags); // "i"
console.log(withGlobal.flags); // "gi"

// Add multiple flags
const regex = /test/;
const enhanced = Sugar.RegExp.addFlags(regex, "gim");
console.log(enhanced.flags); // "gim"

// Adding existing flags is safe (no duplicates)
const alreadyGlobal = /word/g;
const stillGlobal = Sugar.RegExp.addFlags(alreadyGlobal, "g");
console.log(stillGlobal.flags); // "g" (not "gg")

Remove Flags

Creates a new regular expression by removing specified flags.

/**
 * Creates new regex by removing specified flags
 * @param instance - RegExp to modify
 * @param flags - Flags to remove (e.g., "g")
 * @returns New RegExp with specified flags removed
 */
function removeFlags(instance: RegExp, flags: string): RegExp;

Usage Example:

import Sugar from "sugar";

const original = /pattern/gim;
const noGlobal = Sugar.RegExp.removeFlags(original, "g");

console.log(original.flags); // "gim"
console.log(noGlobal.flags); // "im"

// Remove multiple flags
const regex = /test/gims;
const simplified = Sugar.RegExp.removeFlags(regex, "gs");
console.log(simplified.flags); // "im"

// Removing non-existent flags is safe
const simple = /word/i;
const stillSimple = Sugar.RegExp.removeFlags(simple, "g");
console.log(stillSimple.flags); // "i" (no change)

// Remove all flags
const complex = /pattern/gimsy;
const bare = Sugar.RegExp.removeFlags(complex, "gimsy");
console.log(bare.flags); // ""

Common Usage Patterns

Safe String Matching

import Sugar from "sugar";

function createLiteralMatcher(searchTerm) {
  return new RegExp(Sugar.RegExp.escape(searchTerm), "gi");
}

// Safe search in user content
const userQuery = "$100 (special offer)";
const matcher = createLiteralMatcher(userQuery);
const content = "Today only: $100 (special offer) available!";
console.log(matcher.test(content)); // true

Dynamic Flag Management

import Sugar from "sugar";

function toggleCaseSensitive(regex) {
  const flags = Sugar.RegExp.getFlags(regex);
  return flags.includes('i') 
    ? Sugar.RegExp.removeFlags(regex, 'i')
    : Sugar.RegExp.addFlags(regex, 'i');
}

let pattern = /hello/g;
console.log(Sugar.RegExp.getFlags(pattern)); // "g"

pattern = toggleCaseSensitive(pattern);
console.log(Sugar.RegExp.getFlags(pattern)); // "gi"

pattern = toggleCaseSensitive(pattern);
console.log(Sugar.RegExp.getFlags(pattern)); // "g"

RegExp Builder

import Sugar from "sugar";

class RegExpBuilder {
  constructor(pattern) {
    this.regex = new RegExp(pattern);
  }
  
  global() {
    this.regex = Sugar.RegExp.addFlags(this.regex, 'g');
    return this;
  }
  
  ignoreCase() {
    this.regex = Sugar.RegExp.addFlags(this.regex, 'i');
    return this;
  }
  
  multiline() {
    this.regex = Sugar.RegExp.addFlags(this.regex, 'm');
    return this;
  }
  
  build() {
    return this.regex;
  }
}

const emailRegex = new RegExpBuilder('[a-z0-9._%+-]+@[a-z0-9.-]+\\.[a-z]{2,}')
  .global()
  .ignoreCase()
  .build();

console.log(emailRegex.flags); // "gi"

Install with Tessl CLI

npx tessl i tessl/npm-sugar

docs

array.md

date.md

function.md

index.md

localization.md

number.md

object.md

range.md

regexp.md

string.md

tile.json