# sregex

> simple string regular expression that exposes matches in defined variables

Latest version **0.0.5** (published 2013-06-24) · MIT license · 0 weekly downloads

## Install

```sh
npm install sregex
pnpm add sregex
yarn add sregex
bun add sregex
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.5 |
| Published | 2013-06-24 |
| First published | 2013-06-18 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 9 |
| Author | Joseph Werle |
| Maintainers | werle |
| Keywords | string, regex, regular, expression |

## Links

- npm: https://www.npmjs.com/package/sregex
- Repository: https://github.com/jwerle/sregex
- npm.io page: https://npm.io/package/sregex

## Alternatives

- [@mce/gif](https://npm.io/package/@mce/gif.md) — 2.6K weekly downloads
- [cleanse](https://npm.io/package/cleanse.md) — 173 weekly downloads
- [str](https://npm.io/package/str.md) — 127 weekly downloads
- [naming](https://npm.io/package/naming.md) — 95 weekly downloads
- [tap-telco-api](https://npm.io/package/tap-telco-api.md) — 19 weekly downloads

## Recent versions

- 0.0.5 (latest) — 2013-06-24
- 0.0.4 — 2013-06-19
- 0.0.3 — 2013-06-18
- 0.0.2 — 2013-06-18
- 0.0.1 — 2013-06-18

## README

sregex
====

simple string regular expression that exposes matches in defined variables

## install

*node*

```js
$ npm install sregex
```

*component*

```js
$ component install jwerle/sregex
```

*bower*

```js
bower install sregex
```


## usage

using `sregex` is as simple as passing it a string

```js

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

```js

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

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

```js
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
```

the `str` argument for `sregex` accepts a string, but it also can have valid regular expression interpolated

```js
var regex = sregex('it can accept multiple regular expressions like ([a-zA-Z]+), ([0-9]+), and ([a-z]+)');
var values = regex.parse('it can accept multiple regular expressions like foogots, 45, and apple');

console.log(values[0]); // foogots
console.log(values[1]); // 45
console.log(values[2]); // apple
```

it can also accept an instance of `RegExp as input

```js
var regex = sregex(/^foo\s?bar\s?([a-z]+)/);
var values = regex.parse('foo bar noob');

console.log(values[0]); // noob
```

### building a regular expression router

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

```js
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***

```js
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***

```js
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

```js
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

---
_Source: https://npm.io/package/sregex · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
