npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

regee

v0.4.1

Published

Regular expressions with named groups and backreferences with names

Downloads

4

Readme

RegEE

Current Version GitHub release Depedency DevDepedency MIT license

Description

Regular Expression with Extended functionality. RegEE JavaScript module. This module allows the use of named groups in a regular expression.

Changes

  • 0.4.0
    • changed importing syntax
    • added methods: match and replace

Installing module

C:\npm install regee

Usage in code

// for simple import
require('regee');
//or
import 'regee';

// for using as class
const { RegEE } = require('regee')
// or
import { RegEE } from 'regee'

Syntax

|#| method | syntax | meaning | |--|--|--|--| |1| ematch | string.ematch( pattern[, flags] ) | matching string and returns an array| |2| match | string.match( new RegEE( pattern[, flags] ) )|matching string and returns an array | |3| ereplace | string.ereplace( pattern, replacer[, flags] )|replace string and return it | |4| replace | string.replace( new RegEE( pattern[, flags] ), replacer )|replace string and return it |

Parameters

|#| parameter | description | |--|--|--| |1| pattern | Required. The string or regular expression as a string that will be replaced by the replacer. | |2| replacer | Required. New substring or function. | |3| flags | Optional. One of the javascript regexp flags and the added x flag. |

syntax sample:

// returns an array of matches found
str.ematch( pattern[, flags])
// or
str.match(new RegEE(pattern[, flags]))

// returns the result of the replacement as a string
str.ereplace(pattern, replacer[, flags])
// or
str.replace(new RegEE(pattern[, flags]), replacer)

Flag x

|#| character | meaning | |--|--|--| |1|x| ignore whitespace |

example:

var str         = 'My name is John Smith. I am 25 year old';

// or ------------------------------------------------------
var result      = str.ematch(`
    My\\s+name\\s+is
    \\s+(?<FirstName>\\b\\w+\\b)
`, 'x');

// or ------------------------------------------------------
var result      = str.match(new RegEE(`
    My\\s+name\\s+is
    \\s+(?<FirstName>\\b\\w+\\b)
`,'x'));

Pattern

Named group

|#| regexp | meaning | |--|--|--| |1|(?<somename>\w+)|Named group|

example:

var str         = 'My name is John Smith. I am 25 year old';

// or --------------------------------------------------------------------------------
//                                                  name of group
//                                                       |
var result      = str.ematch('My\\s+name\\s+is\\s+(?<FirstName>\\b\\w+\\b)');

// or --------------------------------------------------------------------------------
var result      = str.match(new RegEE('My\\s+name\\s+is\\s+(?<FirstName>\\b\\w+\\b)'));

Named back reference

|#| regexp | meaning | |--|--|--| |1|\k<somename>|Back reference for named group| |2|\g<somename>|Back reference for named group|

example:

var str         = 'to be or not to be';

// or --------------------------------------------------------------------------------
//                           name of group <---------------- backreference
//                                |                                |
var isHamlet    = str.ematch('(?<TB>to\\s+be)\\s+or\\s+not\\s+\\k<TB>', 'i');

// or --------------------------------------------------------------------------------
var isHamlet    = str.match(new RegEE('(?<TB>to\\s+be)\\s+or\\s+not\\s+\\k<TB>', 'i');

Replacer

...as string

String to replace. The string can contain the result value taken from the capture group.

|#| syntax | meaning | |--|--|--| |1|$+{ groupName }| captured value from the named group |

example:

var oldString    = 'My name is John Smith. I am 25 year old';

// or --------------------------------------------------------------------------------
var newString    = str.ereplace('My\\s+name\\s+is\\s+(?<FirstName>\\w+)\\s+(?<LastName>\\w+)\\.\\s+I\\s+am\\s+(?<Age>\\d+)\\s+year\\s+old', '$+{FirstName}: $+{Age}');

// or --------------------------------------------------------------------------------
var newString    = str.replace(new RegEE(`My\\s+name\\s+is
	\\s+(?<FirstName>\\w+)
	\\s+(?<LastName>\\w+)\\.\\s+I\\s+am
	\\s+(?<Age>\\d+)\\s+year\\s+old`, 'x'), '$+{FirstName}: $+{Age}');

// result ----------------------------------------------------------------------------
console.log(newString); // --> John: 25

...as function

The function takes two parameters. The first parameter is a matching string, and the second is an array of captured values. The function returns a string to replace the match string.

|#| syntax | meaning | |--|--|--| |1| function( string, groups ) { ...some code; return '...some string'; } |return a string for replacement | |2| ( string, groups ) => { ...some code; return '...some string'; } |return a string for replacement |

example:

var oldString    = 'My name is John Smith. I am 25 year old.';

// or --------------------------------------------------------------------------------
var newString    = str.ereplace('My\\s+name\\s+is\\s+(?<FirstName>\\w+)\\s+(?<LastName>\\w+)\\.\\s+I\\s+am\\s+(?<Age>\\d+)\\s+year\\s+old', function (match, groups) {
    console.log(match) // --> My name is John Smith. I am 25 year old
    let res = groups.FirstName + ': ' + groups.Age
    return res;
});

// or --------------------------------------------------------------------------------
var newString    = str.replace(new RegEE(`My\\s+name\\s+is
	\\s+(?<FirstName>\\w+)
	\\s+(?<LastName>\\w+)\\.\\s+I\\s+am
	\\s+(?<Age>\\d+)\\s+year\\s+old`, 'x'), function (match, groups) {
        console.log(match) // --> My name is John Smith. I am 25 year old
        let res = groups.FirstName + ': ' + groups.Age
        return res;
});

// or --------------------------------------------------------------------------------
var newString    = str.replace(new RegEE(`My\\s+name\\s+is
	\\s+(?<FirstName>\\w+)
	\\s+(?<LastName>\\w+)\\.\\s+I\\s+am
	\\s+(?<Age>\\d+)\\s+year\\s+old`, 'x'), (match, groups) => groups.FirstName + ': ' + groups.Age);

// result ----------------------------------------------------------------------------
console.log(newString); // --> John: 25

Examples

example matching:

var str = 'My name is John Smith. I am 25 year old. My name is Jasmine. I am 32 year old.';

//You can using method 'ematch' for String objects
var result = str.ematch(`My\\s+name\\s+is
	\\s+(?<FirstName>\\w+)
	\\s+(?<LastName>\\w+)\\.\\s+I\\s+am
	\\s+(?<Age>\\d+)\\s+year\\s+old`, 'gx');

console.log(result[0].FirstName);  // --> John
console.log(result[0][1]);         // --> John
console.log(result[1].FirstName);  // --> Jasmine

console.log(result); // see down...
/*
This action returns arrays, the number of arrays is equal to the number of matches.
The first element of each array contains a string that matches the pattern.

result is...
[
    [
        'My name is John Smith. I am 25 year old',
        'John',
        'Smith',
        '25',
        FirstName: 'John',
        LastName: 'Smith',
        Age: '25'
    ],
    [
        'My name is Jasmine. I am 32 year old',
        'Jasmine',
        undefined,
        '32',
        FirstName: 'Jasmine',
        LastName: undefined,
        Age: '32'
    ]
]
*/

example replacing:

var str = 'My name is John Smith. I am 25 year old. My name is Jasmine. I am 32 year old.';

var newString    = str.replace(new RegEE(`My\\s+name\\s+is
	\\s+(?<FirstName>\\w+)
	\\s+(?<LastName>\\w+)\\.\\s+I\\s+am
	\\s+(?<Age>\\d+)\\s+year\\s+old`, 'gx'), (match, groups) => {
    console.log(match) // --> My name is John Smith. I am 25 year old
    console.log(groups[1]);         // --> John
    console.log(groups.FirstName);  // --> John

    let res = groups.FirstName + ': ' + groups.Age + "\n";
    return res;
});

console.log(newString); // see down...
/*
    John: 25
    Jasmine: 32
*/

Author

Khalid Dudaev

License

MIT License