{"id":20458346,"url":"https://github.com/outatime/pattern-replace","last_synced_at":"2025-09-14T06:09:18.313Z","repository":{"id":14358271,"uuid":"17067995","full_name":"outaTiME/pattern-replace","owner":"outaTiME","description":"Replace text patterns with a given replacement.","archived":false,"fork":false,"pushed_at":"2016-11-21T00:34:42.000Z","size":14,"stargazers_count":5,"open_issues_count":1,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-13T05:45:11.029Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/outaTiME.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE-MIT","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-02-21T20:02:17.000Z","updated_at":"2019-06-04T17:56:23.000Z","dependencies_parsed_at":"2022-08-30T00:01:31.798Z","dependency_job_id":null,"html_url":"https://github.com/outaTiME/pattern-replace","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/outaTiME%2Fpattern-replace","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/outaTiME%2Fpattern-replace/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/outaTiME%2Fpattern-replace/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/outaTiME%2Fpattern-replace/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/outaTiME","download_url":"https://codeload.github.com/outaTiME/pattern-replace/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248670520,"owners_count":21142901,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-15T12:12:04.732Z","updated_at":"2025-04-13T05:45:15.850Z","avatar_url":"https://github.com/outaTiME.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# pattern-replace [![Build Status](https://secure.travis-ci.org/outaTiME/pattern-replace.png?branch=master)](http://travis-ci.org/outaTiME/pattern-replace)\n\nReplace text patterns with a given replacement.\n\n## Install\n\nFirst make sure you have installed the latest version of [node.js](http://nodejs.org/)\n(You may need to restart your computer after this step).\n\nFrom NPM for use as a command line app:\n\n```shell\nnpm install pattern-replace -g\n```\n\nFrom NPM for programmatic use:\n\n```shell\nnpm install pattern-replace\n```\n\nFrom Git:\n\n```shell\ngit clone git://github.com/outaTiME/pattern-replace\ncd pattern-replace\nnpm link .\n```\n\n## API Reference\n\nAssuming installation via NPM, you can use `pattern-replace` in your application like this:\n\n```javascript\nvar fs = require('fs');\nvar Replacer = require('pattern-replace');\nvar options = {\n  patterns: [\n    {\n      match: 'foo',\n      replacement: 'bar'\n    }\n  ]\n};\nvar replacer = new Replacer(options);\nvar contents = '@@foo';\nvar result = replacer.replace(contents);\nconsole.log(result); // bar\n```\n\n### Replacer Options\n\n#### patterns\nType: `Array`\n\nDefine patterns that will be used to replace the contents of source files.\n\n#### patterns.match\nType: `String|RegExp`\n\nIndicates the matching expression.\n\nIf matching type is `String` and `expression` attribute is `false` we use a simple variable lookup mechanism `@@string` (in any other case we use the default regexp replace logic):\n\n```javascript\n{\n  patterns: [\n    {\n      match: 'foo',\n      replacement: 'bar', // replaces \"@@foo\" to \"bar\"\n      expression: false   // simple variable lookup\n    }\n  ]\n}\n```\n\n#### patterns.replacement\nType: `String|Function|Object`\n\nIndicates the replacement for match, for more information about replacement check out the [String.replace].\n\nYou can specify a function as replacement. In this case, the function will be invoked after the match has been performed. The function's result (return value) will be used as the replacement string.\n\n```javascript\n{\n  patterns: [\n    {\n      match: /foo/g,\n      replacement: function () {\n        return 'bar'; // replaces \"foo\" to \"bar\"\n      }\n    }\n  ]\n}\n```\n\nAlso supports object as replacement (we create string representation of object using [JSON.stringify]):\n\n```javascript\n{\n  patterns: [\n    {\n      match: /foo/g,\n      replacement: [1, 2, 3] // replaces \"foo\" with string representation of \"array\" object\n    }\n  ]\n}\n```\n\n[String.replace]: http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace\n[JSON.stringify]: http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify\n\n#### patterns.json\nType: `Object`\n\nIf an attribute `json` found in pattern definition we flatten the object using `delimiter` concatenation and each key–value pair will be used for the replacement (simple variable lookup mechanism and no regexp support).\n\n```javascript\n{\n  patterns: [\n    {\n      json: {\n        \"key\": \"value\" // replaces \"@@key\" to \"value\"\n      }\n    }\n  ]\n}\n```\n\nAlso supports nested objects:\n\n```javascript\n{\n  patterns: [\n    {\n      json: {\n        \"key\": \"value\",   // replaces \"@@key\" to \"value\"\n        \"inner\": {        // replaces \"@@inner\" with string representation of \"inner\" object\n          \"key\": \"value\"  // replaces \"@@inner.key\" to \"value\"\n        }\n      }\n    }\n  ]\n}\n```\n\n#### patterns.yaml\nType: `String`\n\nIf an attribute `yaml` found in pattern definition we flatten the object using `delimiter` concatenation and each key–value pair will be used for the replacement (simple variable lookup mechanism and no regexp support).\n\n```javascript\n{\n  patterns: [\n    {\n      yaml: 'key: value'  // replaces \"@@key\" to \"value\"\n    }\n  ]\n}\n```\n\n#### patterns.expression\nType: `Boolean`\nDefault: `false`\n\nIndicates the type of matching.\n\nIf detects regexp instance in `match` attribute, we assume to works with expression matcher (in any other case should be forced).\n\n#### variables\nType: `Object`\n\nThis is the old way to define patterns using plain object (simple variable lookup mechanism and no regexp support), you can still using but for more control you should use the new `patterns` way.\n\n```javascript\n{\n  variables: {\n    'key': 'value' // replaces \"@@key\" to \"value\"\n  }\n}\n```\n\n#### prefix\nType: `String`\nDefault: `@@`\n\nThe prefix added for matching (prevent bad replacements / easy way).\n\n\u003e This only applies for simple variable lookup mechanism.\n\n#### usePrefix\nType: `Boolean`\nDefault: `true`\n\nIf set to `false`, we match the pattern without `prefix` concatenation (useful when you want to lookup an simple string).\n\n\u003e This only applies for simple variable lookup mechanism.\n\n#### preservePrefix\nType: `Boolean`\nDefault: `false`\n\nIf set to `true`, we preserve the `prefix` in target.\n\n\u003e This only applies for simple variable lookup mechanism and `patterns.replacement` is an string.\n\n#### delimiter\nType: `String`\nDefault: `.`\n\nThe delimiter used to flatten when using object as replacement.\n\n#### preserveOrder\nType: `Boolean`\nDefault: `false`\n\nIf set to `true`, we preserve the patterns definition order, otherwise these will be sorted (in ascending order) to prevent replacement issues like `head` / `header` (typo regexps will be resolved at last).\n\n### Usage Examples\n\n#### Basic\n\nFile `src/manifest.appcache`:\n\n```\nCACHE MANIFEST\n# @@timestamp\n\nCACHE:\n\nfavicon.ico\nindex.html\n\nNETWORK:\n*\n```\n\nNode:\n\n```js\nvar fs = require('fs');\nvar Replacer = require('pattern-replace');\nvar options = {\n  patterns: [\n    {\n      match: 'timestamp',\n      replacement: new Date().getTime()\n    }\n  ]\n};\nvar replacer = new Replacer(options);\nvar contents = fs.readFileSync('./src/manifest.appcache').toString();\nvar result = replacer.replace(contents);\nconsole.log(result); // replaced output\n```\n\n#### Multiple matching\n\nFile `src/manifest.appcache`:\n\n```\nCACHE MANIFEST\n# @@timestamp\n\nCACHE:\n\nfavicon.ico\nindex.html\n\nNETWORK:\n*\n```\n\n\nFile `src/humans.txt`:\n\n```\n              __     _\n   _    _/__  /./|,//_`\n  /_//_// /_|///  //_, outaTiME v.@@version\n\n/* TEAM */\n  Web Developer / Graphic Designer: Ariel Oscar Falduto\n  Site: http://www.outa.im\n  Twitter: @outa7iME\n  Contact: afalduto at gmail dot com\n  From: Buenos Aires, Argentina\n\n/* SITE */\n  Last update: @@timestamp\n  Standards: HTML5, CSS3, robotstxt.org, humanstxt.org\n  Components: H5BP, Modernizr, jQuery, Twitter Bootstrap, LESS, Jade, Grunt\n  Software: Sublime Text 2, Photoshop, LiveReload\n\n```\n\nNode:\n\n```js\nvar fs = require('fs');\nvar Replacer = require('pattern-replace');\nvar options = {\n  patterns: [\n    {\n      match: 'version',\n      replacement: '0.1.0'\n    },\n    {\n      match: 'timestamp',\n      replacement: new Date().getTime()\n    }\n  ]\n};\nvar replacer = new Replacer(options);\nvar contents = fs.readFileSync('./src/manifest.appcache').toString();\nvar result = replacer.replace(contents);\nconsole.log(result); // replaced output\ncontents = fs.readFileSync('./src/humans.txt').toString();\nresult = replacer.replace(contents);\nconsole.log(result); // replaced output\n```\n\n#### Cache busting\n\nFile `src/index.html`:\n\n```html\n\u003chead\u003e\n  \u003clink rel=\"stylesheet\" href=\"/css/style.css?rel=@@timestamp\"\u003e\n  \u003cscript src=\"/js/app.js?rel=@@timestamp\"\u003e\u003c/script\u003e\n\u003c/head\u003e\n```\n\nNode:\n\n```js\nvar fs = require('fs');\nvar Replacer = require('pattern-replace');\nvar options = {\n  patterns: [\n    {\n      match: 'timestamp',\n      replacement: new Date().getTime()\n    }\n  ]\n};\nvar replacer = new Replacer(options);\nvar contents = fs.readFileSync('./src/index.html').toString();\nvar result = replacer.replace(contents);\nconsole.log(result); // replaced output\n```\n\n#### Include file\n\nFile `src/index.html`:\n\n```html\n\u003cbody\u003e\n  @@include\n\u003c/body\u003e\n```\n\nNode:\n\n```js\nvar fs = require('fs');\nvar Replacer = require('pattern-replace');\nvar options = {\n  patterns: [\n    {\n      match: 'include',\n      replacement: fs.readFileSync('./includes/content.html').toString()\n    }\n  ]\n};\nvar replacer = new Replacer(options);\nvar contents = fs.readFileSync('./src/index.html').toString();\nvar result = replacer.replace(contents);\nconsole.log(result); // replaced output\n```\n\n#### Regular expression\n\nFile `src/username.txt`:\n\n```\nJohn Smith\n```\n\nNode:\n\n```js\nvar fs = require('fs');\nvar Replacer = require('pattern-replace');\nvar options = {\n  patterns: [\n    {\n      match: /(\\w+)\\s(\\w+)/,\n      replacement: '$2, $1' // replaces \"John Smith\" to \"Smith, John\"\n    }\n  ]\n};\nvar replacer = new Replacer(options);\nvar contents = fs.readFileSync('./username.txt').toString();\nvar result = replacer.replace(contents);\nconsole.log(result); // replaced output\n```\n\n#### Lookup for `foo` instead of `@@foo`\n\nThe `String` matching type or `expression` in `false` generates a simple variable lookup mechanism `@@string`, to skip this mode use one of the below rules ... make your choice:\n\nNode:\n\n```js\nvar Replacer = require('pattern-replace');\n\n// option 1 (explicitly using an regexp)\nvar replacer_op1 = new Replacer({\n  patterns: [\n    {\n      match: /foo/g,\n      replacement: 'bar'\n    }\n  ]\n});\n\n// option 2 (easy way)\nvar replacer_op2 = new Replacer({\n  patterns: [\n    {\n      match: 'foo',\n      replacement: 'bar'\n    }\n  ],\n  usePrefix: false\n});\n\n// option 3 (old way)\nvar replacer_op3 = new Replacer({\n  patterns: [\n    {\n      match: 'foo',\n      replacement: 'bar'\n    }\n  ],\n  prefix: '' // remove prefix\n});\n```\n\n## Command Line\n\n_(Coming soon)_\n\n## Release History\n\n * 2014-03-11   v0.1.2   New pattern matching for YAML object. New preserveOrder flag.\n * 2014-02-26   v0.1.1   Remove the force flag (only applies in grunt plugin).\n * 2014-02-25   v0.1.0   Initial version.\n\n---\n\nTask submitted by [Ariel Falduto](http://outa.im/)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Foutatime%2Fpattern-replace","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Foutatime%2Fpattern-replace","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Foutatime%2Fpattern-replace/lists"}