Remove Whitespaces After A Certain Character - Regex

I’m looking for a regular expression for the following scenario. I’m trying to remove all whitespaces after a certain character. I’m trying to rename lots of files in a specific manner. For example:

I have the filename:
“SYS022_02 Stardust_No Vocals.wav”
“SYS022_06 If You Build It He Will Come_Act 3 No Orchestra Mix.wav”

But I’d like them to read:
“SYS022_02 Stardust_NoVocals.wav”
“SYS022_06 If You Build It He Will Come_Act3NoOrchestraMix.wav”

So basically I’d like to eliminate all whitespaces after the second underscore “_”. Anybody have a regular expression for this scenario? Been trying for hours. Thanks for any help!

Below is a picture to help explain what I’m trying to do. I’ve built a custom action list to rename my thousands of library audio files, but I’m stuck on the last step which is trying to remove any spaces after the underscore in the filename. The software allows the use of regular expressions, and I have some knowledge of expression language, but I just can’t seem to crack this one.

Thanks again for any help!

I suggest looking into capture groups.

Here is an example of something I came up with quickly. You might be able to refactor it to avoid using replace twice, but I will leave that to you.

function replacer(_, p1) {
  return p1.replace(/\s/g, '');

var testStrings = [
  "SYS022_02 Stardust_No Vocals.wav",
  "SYS022_06 If You Build It He Will Come_Act 3 No Orchestra Mix.wav"

testStrings.forEach(function(str) {
  var newStr = str.replace(/([^_]+)$/, replacer);

Basically, I specify a function for the replacement. You can read more about doing so here.

1 Like