map-tokens
Register patterns to find tokens in a string and map them to unique IDs, allowing them to be extracted, replaced or restored.
Install
npm
Install withnpm i map-tokens --save
Run tests
npm test
Usage
var Tokens = ;var tokens = 'foo "bar" baz quux'; // tokenize anything in double quotestokens; // replace tokens with temporary IDstokens;//=> 'foo "__TOKEN_IDFOBV3I__" baz quux' // Run a replacement on the string while the IDs are in place.// Even though our regex specifies `bar`, `bar` won't be replaced// since it was replaced with an ID.tokenscontent = tokenscontent;//=> 'AAA "__TOKEN_IDFOBV3I__" baz quux' // last, retore the tokenstokens;console;//=> 'AAA "bar" baz quux'
Or, you can save the result of .extract()
and explicitly pass the result to the .restore()
method later:
var tokens = ; tokens; console;//=> 'abc def ghi jkl'
API
Tokens
Create an instance of Tokens
with the given string
.
string
{String}
var Tokens = ;var tokens = string;
._input
Register a regex
pattern to use for matching tokens.
regex
{RegExp}
tokens;
.pattern
Register a regex
pattern to use for matching tokens.
regex
{RegExp}
tokens;
.extract
Run the registered patterns on the input
string, which inserts tokens in place of matching strings.
patterns
{Array}: Optionally pass an array of regex patterns to use
tokens;// ortokens;
.restore
Restore previously inserted tokens.
obj
{Object}: Optionally pass an object withtokens
andcontent
properties to use.
tokens;// ortokens;
Contributing
Pull requests and stars are always welcome. For bugs and feature requests, please create an issue
Author
Jon Schlinkert
License
Copyright (c) 2014 Jon Schlinkert
Released under the MIT license
This file was generated by verb on November 27, 2014.