I’m having trouble with a regular expression, I have several images with file name that need changing. I’ve done them by hand. It was quick easy and painless. However, I wanted to know what I needed to do as a simple replacement reg ex using JavaScript. And that’s when it doesn’t quite work out. The image is called “muti blossom 02.png” and it’s going to be re-sized and saved out as JPEGs with the name “iOS_multi_BLOSSOM_2048.jpg”. The others are of the same form but have different nouns; winter, leaf, circus etc.
The file-name is structured as follows:
- “mutli” at the start (lower case),
- white space,
- the noun (lower case),
- white-space,
- a number (that may have a preceding 0 and may be one or two digits),
- file extension which may be .png or .psd (lowercase).
It then needs to be changed to:
- iOS_multi (camel case as written),
- noun (UPPERCASE),
- 2048 (new fixed size),
- new file extension .jpg(lowercase).
I know that ([a-z]+\s) matches “multi” and that (\s\d+.[a-z]+$) will match the numbers and file extension, but have no idea how to successfully match the bit in the middle as well. And do the uppercase on the noun. But I’m sure there is someone else that does. Thank you.
In JavaScript regex you cannot do this with a
replaceas it is not possible to uppercase the replacement text. However thematchmethod will return an array which you can then manipulate.Note: this assumes that the “noun” is a single word with no spaces