Skip to content

Instantly share code, notes, and snippets.

@kvendrik
Last active April 12, 2016 10:54
Show Gist options
  • Select an option

  • Save kvendrik/b8e79ebf4d6636a44beb33871abdef1b to your computer and use it in GitHub Desktop.

Select an option

Save kvendrik/b8e79ebf4d6636a44beb33871abdef1b to your computer and use it in GitHub Desktop.
Named Regex Captures in JS
{
"presets": ["es2015"]
}

NamedRegex.js

Isomorphic library for named regex captures in js

A small isomorphic library that lets you name regex capture groups.

Named Regex Captures?

Named capturing lets you name the captures you do in your regex matches. So instead of having to get the captured strings from a regex match out of an array using their index you can get them using their name.

This is a feature that is implemented natively in a lot of other languages like Ruby, PHP and Perl but not in JavaScript yet, thats why this library is here.

With Native JS

let m = 'user 271 tel 789-218-212'.match(/user (\d+) tel ([0-9\-]+)/);
if(m && m[1] && m[2])

With NamedRegex

let m = new NamedRegex(/user (:<id>\d+) tel (:<tel>[0-9\-]+)/);
if(m.id && m.tel)

How to use

  1. npm install namedregex --save
  2. Import and use
import NamedRegex from 'namedregex';

let namedRegex = new NamedRegex(/user (:<id>\d+)/);
namedRegex.match('user 218'); //{ id: 218 }
import NamedRegex from './';
let namedRegex = new NamedRegex(/user\s?(:<id>\d+)? category (:<category>\d+)\s?(:<repoId>\d+)?/);
console.log(
namedRegex.match('user 581 category 218 6718'),
namedRegex.match('user category 218'),
namedRegex.match('socks')
);
export default class NamedRegex {
constructor(regex){
let regexStr = String(regex);
//rm regex slashes from str
regexStr = regexStr.replace(/^\//, '').replace(/\/(\w+)$/, '');
this._paramNames = this._getParams(regexStr);
this._cleanRegex = this._getCleanRegex(regexStr);
}
_getCleanRegex(regexStr){
let cleanStr = regexStr.replace(/(\()\:\<\w+\>/g, '$1');
return new RegExp(cleanStr);
}
_getParams(regexStr){
let params = regexStr.match(/\(\:\<\w+\>/g),
paramNames = [];
if(params && params.length > 0){
for(let param of params){
let nameMatch = param.match(/\(\:\<(\w+)\>/);
if(nameMatch && nameMatch[1]){
paramNames.push(nameMatch[1]);
}
}
}
return paramNames;
}
_matchCapturesWithNames(captures){
let paramNames = this._paramNames,
results = {};
for(let i = 1; i < captures.length; i++){
let val = captures[i],
key = paramNames[i-1];
if(val && key){
results[key] = val;
}
}
return results;
}
match(str){
let captures = str.match(this._cleanRegex),
results = {};
if(captures){
results = this._matchCapturesWithNames(captures);
}
return results;
}
}
{
"name": "namedregex",
"version": "0.1.0",
"description": "",
"main": "dist/NamedRegex.js",
"scripts": {
"build": "babel src/index.js -o dist/NamedRegex.js",
"examples": "npm build && babel-node examples.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Koen Vendrik <[email protected]>",
"license": "MIT",
"devDependencies": {
"babel-cli": "^6.6.5",
"babel-preset-es2015": "^6.6.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment