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

sregex

v0.0.5

Published

simple string regular expression that exposes matches in defined variables

Downloads

252

Readme

sregex

simple string regular expression that exposes matches in defined variables

install

node

$ npm install sregex

component

$ component install jwerle/sregex

bower

bower install sregex

usage

using sregex is as simple as passing it a string


var regex = sregex('some string')

regex.test('some string'); // true
regex.test('some other string'); // false

the returned regex object from sregex is an instance of RegExp with a parse() function attached to it


regex.parse('some string'); // {}

you can easily define variables within your string sregex string and access them from a string with the parse function

var regex = sregex('my name is :name and i am :age');
var values = regex.parse('my name is joe and i am 22');

console.log(values.name); // joe
console.log(values.age); // 22

building a regular expression router

building a router that parses url parameters can be simple as well

var http = require('http')
	,	sregex = require('sregex')

http.createServer(function (req, res) {
	var regex = sregex('/:resource/:id/:action')

	// in the browser head to `http://localhost:4000/videos/1234/edit`
	console.log(regex.parse(req.url)); // { resource: 'videos', id: '1234', action: 'edit' }
}).listen(4000);

api

sregex(str)

converts a string to regular expression and allows retrieval of defined variables when parsing

  • str - a string to convert to regular expression

example

var regex = sregex('/user/:id')

.parse(str)

parses a given string and returns an object representing the values extracted using the regular expression used to create it

  • str - a string to parse and extact values from based on regular expression matches

example

var regex = sregex('/account/:action')
var values = regex.parse('/action/edit');

console.log(values.action); // edit

example

we can convert a object to a JSON string and then to binary and attach it to the string

var regex = sregex('payload|:data')

var bytes = [];

var data = {
	id: 1234,
	date: Date.now()
};

var str = JSON.stringify(data)

for (var i = 0; i < str.length; ++i) {
	bytes.push(str.charCodeAt(i));
}

var values = regex.parse('payload|'+ bytes.toString());

console.log(values.data); // '123,34,105,100,34,58,49,50,51,52,44,34,100,97,116,101,34,58,49,51,55,49,53,56,51,52,51,53,52,52,48,125'

// parse it back into a JSON string

var parsed = ''
values.data.split(',').map(function (part) {
	parsed += String.fromCharCode(part);
});

console.log(parsed); // {"id":1234,"date":1371583641484}
console.log(JSON.parse(parsed)); // { id: 1234, date: 1371583754259 }

license

MIT