{"id":16818984,"url":"https://github.com/bomsy/grunt-regex-replace","last_synced_at":"2025-03-22T03:31:29.774Z","repository":{"id":5711495,"uuid":"6922532","full_name":"bomsy/grunt-regex-replace","owner":"bomsy","description":"Grunt plugin to search and replace text content of files based on regular expression patterns","archived":false,"fork":false,"pushed_at":"2017-10-26T12:35:12.000Z","size":3601,"stargazers_count":30,"open_issues_count":0,"forks_count":14,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-18T07:43:09.216Z","etag":null,"topics":["grunt","grunt-plugins","javascript","regex"],"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/bomsy.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":"2012-11-29T14:27:58.000Z","updated_at":"2020-06-24T08:13:50.000Z","dependencies_parsed_at":"2022-07-10T21:46:12.513Z","dependency_job_id":null,"html_url":"https://github.com/bomsy/grunt-regex-replace","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bomsy%2Fgrunt-regex-replace","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bomsy%2Fgrunt-regex-replace/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bomsy%2Fgrunt-regex-replace/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bomsy%2Fgrunt-regex-replace/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bomsy","download_url":"https://codeload.github.com/bomsy/grunt-regex-replace/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244902929,"owners_count":20529114,"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":["grunt","grunt-plugins","javascript","regex"],"created_at":"2024-10-13T10:51:42.521Z","updated_at":"2025-03-22T03:31:29.410Z","avatar_url":"https://github.com/bomsy.png","language":"JavaScript","readme":"# grunt-regex-replace\n\n[![NPM](https://nodei.co/npm/grunt-regex-replace.png?downloads=true\u0026downloadRank=true\u0026stars=true)](https://npmjs.org/package/grunt-regex-replace)\n\nGrunt plugin to search and replace text content of files based on regular expression patterns\n\n## Getting Started\nInstall this grunt plugin next to your project's [grunt.js gruntfile][getting_started] with:\n\n```\nnpm install --save-dev grunt-regex-replace\n```\n\nThen add this line to your project's `grunt.js` gruntfile:\n\n```javascript\ngrunt.loadNpmTasks('grunt-regex-replace');\n```\n\n[grunt]: http://gruntjs.com/\n[getting_started]: https://github.com/gruntjs/grunt/blob/master/docs/getting_started.md\n\n## How to use\nHere is a sample of the definition within the object passed to grunt.initConfig\n\n### Sample Code\n\n```js\n\"regex-replace\": {\n    foofoo: { //specify a target with any name\n        src: ['foo/bar.js'],\n        actions: [\n            {\n                name: 'bar',\n                search: '(^|\\\\s)console.log',\n                replace: '//console.log',\n                flags: 'g'\n            },{\n                name: 'foo',\n                search: 'var v = \\'[^\\']*\\';',\n                replace: 'var v = \\'\u003c%= pkg.release.version_code %\u003e\\';',\n                flags: ''\n            },{\n               name: 'foobar',\n               search: new RegExp('\\\\w+'),\n               replace: function() {\n                    return 'foofoo';\n               }\n            },{\n               name: 'baz',\n               use: function(data) {\n                 return data.sourceContent.length \u003e 3;\n               },\n               search: 'abc',\n               replace: 'abcde'\n            }\n        ]\n    }\n}\n```\n\n### src property\nTakes the path to the files relative to the grunt file, it accepts strings as well as an array of file paths.\nAlso supports templates, e.g\n\n```js\nsrc: 'customisation/*.js',\nsrc: '**/*.js',\nsrc: ['foo/bar.js','foo/foo.js'],\nsrc: ['\u003c%= pkg.id %\u003e/bar.js', 'foo/foo.js']\n```\n\n### dest property\nTakes a file path string or an array of file paths that match the src paths. If a `dest` file is specified for the corresponding\n`src` file, the `src` file remains unchanged and the changes are applied to the `dest` file. e.g\n\n1) `bla.js` will not be overwritten but `foo.js` will contain the new changes.\n\n```js\n {\n    src: ['bla.js'],\n    dest: 'foo.js'\n }\n```\n\n2) `bla.js` and `foo.js` will be affected as above. `baz.js` will be overwritten.\n\n```js\n {\n    src: ['bla.js', 'baz.js'],\n    dest: ['foo.js']\n }\n```\n\n3) `bla.js` and  `foo.js` will be affected as above. `baz.js` will be ignored.\n\n```js\n {\n    src: ['bla.js'],\n    dest: ['foo.js', 'baz.js']\n }\n```\n\n### actions property (array | function)\nAccepts an array of objects or a function (which returns an array of objects) representing the actions to take place. Each action contains an optional `name` property, an optional `use` property, a `search` property, a `replace` property and\nan optional `flags` property. Here are some examples of the object.\n\n```js\n{\n    name: 'foo',\n    use: function(data) {\n      return data.file.indexOf('.skip') === -1 \u0026\u0026 data.sourceContent.indexOf('console.log') \u003e -1;\n    }, //also accepts a template string or any value\n    search: '(^|\\\\s)console.log',\n    replace: '//console.log',\n    flags: 'gi'\n}\n\n{\n    name: 'bar',\n    search: /\\\\w+/g, //also accepts new RegExp()\n    replace: function() {\n        return 'foo';\n    }\n}\n```\n\n#### name property\nA string value.\n\n#### use property (function | template string | value)\n*Default*: `true`\n\nUsed to determine whether the corresponding action should be executed or not. If set to `true` the action executes,\nif `false` it does not. Can also be a function which returns a value, a template string producing a value.\nIt enables specifying the conditions for when actions are used.\n\nAn object with the following fields is passed in test function:\n\n* `file` - path to a file that is being processed;\n* `sourceContent` - source contents of the processed file;\n* `updatedContent` - contents of the file after previously applied actions;\n* `action` - object that is representing options of the current action;\n* `task` - reference to the corresponding task;\n* `grunt` - reference to grunt.\n\n#### search property (regexp | substr)\nA regular expression string or object defining the text content to be found.\n\n#### replace property (substr | function)\nA string / regular expression pattern or function to replace the text content.\nFor the replace function, values that match the parenthesized substring matches are passed as arguments\n```js\n{\n    search: new RegExp(/(\\w+)\\s(\\w+)/),\n    replace: function(arg1, arg2, ... argN) {\n      // arg1 is the full string matched\n      // arg2 is the first parenthesized substring match\n      // argN is the Nth parenthesized substring match\n    }\n}\n```\nSee [MDN Documentation](https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions#Using_parenthesized_substring_matches) for details on \"using parenthesized substring matches.\"\n\n#### flags property\nRegular expressions options (ie `gmi`). If the flags property is not defined, and the search property is a string, it defaults to `'g'`. To specify no options, set the flags to empty string (ie flags : '').\nNote: Do not use the `flags` property if a `regexp` was used for the search property. Instead, use the flag(s) in your regex. ie: `/^[a-z0-9_-]{6,18}$/g`\n\n#### debug log\nspecify `--verbose` as a command-line option to show detailed logs\n\n## Contributing\nIn lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using [grunt][grunt].\n\n## Release History\n* `v0.4.0` - added support for `dest` property\n* `v0.3.0` - added new `use` property to `actions`, removed peerDependencies restrictions\n* `v0.2.10` - add verbose logging\n* `v0.2.9` - Clarification for regex flags usage\n* `v0.2.7` - Support for passing a function to the action property, Updated documentation for using parenthesized substring matches\n* `v0.2.6` - Support for file globbing patterns.\n* `v0.2.5` - fix /bin not exist error\n* `v0.2.4` - added name property, search property now supports regexp object, replace property now supports functions.\n* `v0.2.3` - task format fixes for compatibilty with 0.4.0.\n* `v0.2.2` - version fixes\n* `v0.2.1` - Updated to support grunt 0.4.x\n* `v0.1.2` - Changes to readme\n* `v0.1.1` -\n* `v0.1.0` - First Release\n\n## License\n\nCopyright (c) 2012 Hubert Boma Manilla  \nLicensed under the MIT license.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbomsy%2Fgrunt-regex-replace","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbomsy%2Fgrunt-regex-replace","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbomsy%2Fgrunt-regex-replace/lists"}