Register patterns to find tokens in a string and map them to unique IDs, allowing them to be extracted, replaced or restored.
npm i map-tokens --save
npm test
var Tokens = require('map-tokens');
var tokens = new Tokens('foo "bar" baz quux');
// tokenize anything in double quotes
tokens.pattern(/\"([^\"]+?)\"/);
// replace tokens with temporary IDs
tokens.extract();
//=> '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.
tokens.content = tokens.content.replace(/foo|bar/g, 'AAA');
//=> 'AAA "__TOKEN_IDFOBV3I__" baz quux'
// last, retore the tokens
tokens.restore();
console.log(tokens.result);
//=> 'AAA "bar" baz quux'
Or, you can save the result of .extract() and explicitly pass the result to the .restore() method later:
var tokens = new Tokens();
tokens.restore('abc __TOKEN_ID9H8AY1__ __TOKEN_IDLDO84N__ jkl', {
tokens: {
__TOKEN_ID9H8AY1__: 'def',
__TOKEN_IDLDO84N__: 'ghi'
}
});
console.log(tokens.result);
//=> 'abc def ghi jkl'
Create an instance of Tokens with the given string.
string {String}var Tokens = require('map-tokens');
var tokens = new Tokens(string);
Register a regex pattern to use for matching tokens.
regex {RegExp}tokens.pattern(/def/);
Register a regex pattern to use for matching tokens.
regex {RegExp}tokens.pattern(/def/);
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 usetokens.extract();
// or
tokens.extract([/def/]);
Restore previously inserted tokens.
obj {Object}: Optionally pass an object with tokens and content properties to use.tokens.restore();
// or
tokens.restore(obj);
Pull requests and stars are always welcome. For bugs and feature requests, please create an issue
Jon Schlinkert
Copyright (c) 2014 Jon Schlinkert
Released under the MIT license
This file was generated by verb on November 27, 2014.