

The use of these expressions is primarily meant for power users and people with programming experience but none the less gaining knowledge of the basics will. Advanced Renamer supports the use of regular expressions for pattern searching and replacing in several methods. However you can chain the two commands using the & operator instead to form one npm script - the single line will be rather long though :)įor Solution B, I often utilize version 0.6.1 of renamer, so I run npm i -D to install, as I typically have older versions of nodejs to support. A regular expression language is a powerful way of manipulating with texts.

Also, in this example the -dry-run option was removed, so reinstate it for testing purposes.įor both Solution A and Solution B, the copying and renaming logic has been added to separate npm scripts, (namely copy and rename respectively) for clarity of explanation. it's now wrapped in a leading and trailing forward slash. Note: The -regex option has been omitted and a regexp is now a literal, i.e. This change to the API results in the rename script, (as previously shown), needing to be redefined as follows: "rename": "renamer -f \"/^$/\" -r \"conf.json\" \"dist/config/*\"",

A regular expression literal is now provided since this version instead. There was a breaking change since v0.7.0 which included the removal of the -regex option (see here for further info). If the version of renamer installed is >=0.7.0 Simply omit the -dry-run option from your script and run the command again for the actual file name to be changed. You'll also notice that the actual filename of the copied file hasn't changed, that's because we included the -dry-run option. If the version of renamer installed is dist\config\conf.json It differs slightly if the version installed is =0.7.0: Note: The reason I ask you to check which version was installed is because this will determine which of the following renamer commands you should utilize. Then run the following command to check which version of renamer was installed.

#Regex renamer install#
Install and check which version:Ĭd to your project directory and install renamer by running the following command: npm i -D renamer This may be beneficial if your renaming requirements become more complex than the example provided in your question, or if you want something less verbose than Solution A. You could also consider installing and utilizing renamer for renaming the file. On successful completion you should see the following logged to the console after the file has been copied and renamed: Then run the following npm command: npm run copy-and-rename "copy-and-rename": "npm run copy & npm run rename" \".\".įor instance, configure the scripts section of your package.json as follows: "scripts": )\"", To remove this use a second replace method with the regex: " $" - where "$" indicates that the space must be followed by the end of the string.Below are a couple of solutions to successfully meet your requirement: Solution AĪs noted in answer the callback parameter is mandatory when using the nodejs built-in fs.rename().Īlso when utilizing the node -e command via a npm script it is necessary to wrap the script to evaluate in JSON escaped double quotes, i.e. This regex will leave an unwanted space at the end of filenames that end within a string within parentheses (ie the additional space that preceded the "(" "*" matches any number of characters except those listed after "^" - ie match everything up to the next ")"įinally " ?" matches zero or one trailing spaces Read the basic introduction to regular expressions in the User Guide:įor an explanation, first consider the removal only of strings within parentheses.įirst note that characters such as "(" and "[" etc have special meanings in a regex and must be "escaped" by prefixing with "\" in order to represent themselves. Replace with nothing and check "Use regular expressions" in each case and do not include the quotation marks.
