regexp-comment

1.0.3 • Public • Published

Description

A lightweight RegExp (?#) comment syntax extend.

This library aims at supporting the syntax of comment (?#) in regex. It supports both native RegExp and pure-regex.

Tutorial

Installation

npm i regexp-comment

Import

var regComment = require("regexp-comment")

Or use import (in node.js or browser with a module bundler or loader):

import regComment from "regexp-comment"

In browser (exports as regComment) (Experimental):

<script src="https://cdn.jsdelivr.net/npm/regexp-comment@latest/dist.umd.cjs"></script>

Example

Comment at the beginning or the end

var regex = "(\\d+)(?# It will match a number.)"
var source = new regComment(regex)
console.log(source)
// "(\\d+)"

Comment insides any group

var regex = "([a-z](?# match a lowercase letter.)\\d+(?# Then match a number.))"
var source = new regComment(regex)
console.log(source)
// "([a-z](?:)\\d+(?:))"

Comment at regExp literal

var regex = /(\\d+)(\?# It will match a number.)/
var source = new regComment(regex)
console.log(source)
// "(\\d+)"

Multiline comments

var regex = `([A-Z][A-Za-z]*)(?#
Match a capitalized word.
For example, it will hit "Attempt".
)`
var source = new regComment(regex)
console.log(source)
// "([A-Z][A-Za-z]*)"

Api

regComment(source | regex[, flags[, opts]]): string

  • opts:
    • unescaped: true Indicate whether to unescape the "(\\?#)" to "(?#)". It's useful for writing comments in regExp literal, like /a(\?#This is a comment)/.

Transpile a regex pattern with comment syntax, and return a source string that can be passed in RegExp and PureRegex constructor.

Package Sidebar

Install

npm i regexp-comment

Weekly Downloads

0

Version

1.0.3

License

ISC

Unpacked Size

5.45 kB

Total Files

4

Last publish

Collaborators

  • x_g