How to Replace RegEx Group with uppercase?

Javascript renaming examples. Javascript renaming is supported in version 3 or newer.

How to Replace RegEx Group with uppercase?

Postby dlzh » Fri Mar 11, 2022 6:01 pm

For whatever reason, I cannot get this to work. The character simply stays as a lowercase character. If I replace the variable "match" with an actual lowercase character, it will convert it, so my guess is that the passed variable is not a string or a type that it can convert. I even tried converting "match" to a string, and the result did not change. I am sure this is very simple, and I'm overlooking some basic JS syntax. ** Note: This code is verbose for troubleshooting. **

Sample Filename:
salt & pepper

Expected Result:
salt & Pepper

JavaScript Code:
Code: Select all
function upperCase(match) {
  var result = match.toUpperCase();
  return result;
}

newName = newName.replace(/\& ([a-z])/g, '& ' + upperCase('$1'));
dlzh
 
Posts: 3
Joined: Fri Mar 11, 2022 5:49 pm

Re: How to Replace RegEx Group with uppercase?

Postby Luuk » Sat Mar 12, 2022 4:05 pm

When using the replace(/regex-match/g, "replace") can just use a function inside for the "replace" like...
newName = name.replace(/\& ([a-z])/g, function(x) {return x.toUpperCase()} )
Luuk
 
Posts: 690
Joined: Fri Feb 21, 2020 10:58 pm

Re: How to Replace RegEx Group with uppercase?

Postby dlzh » Sat Mar 12, 2022 5:05 pm

@Luuk: With "can just use," are you saying:
1) "You may/should use," meaning that instead of using a function outside like I have I *may also use* a function inside. If so, that was why I added the note about the code being verbose.
2) "You can only use," meaning that using a function outside like I have will not work.

If the answer is #2, why is that the case? The external function is called and does work; therefore, the issue should be how the RegEx group is passed.
dlzh
 
Posts: 3
Joined: Fri Mar 11, 2022 5:49 pm

Re: How to Replace RegEx Group with uppercase?

Postby Admin » Wed Mar 16, 2022 2:56 am

Code: Select all
newName = newName.replace(/\& ([a-z])/g, '& ' + upperCase('$1'));


I think you probably want to use:

Code: Select all
newName = name.replace(/\& ([a-z])/g, '& ' + upperCase('$1'));


as name is the current name and newName is the new name.
Admin
Site Admin
 
Posts: 2343
Joined: Tue Mar 08, 2005 8:39 pm

Re: How to Replace RegEx Group with uppercase?

Postby dlzh » Thu Mar 17, 2022 3:22 am

@Admin Yes. I probably should have changed that for the example purposes here. I actually have a whole series of these line after line, so the first one does use "newName = name..." and then the rest have to operate on changing the "newName" each time.

Do you have any suggestions about my actual question? I still cannot get it to work with '$1' but if you send a character (e.g., 'n'), it will capitalize it fine.
dlzh
 
Posts: 3
Joined: Fri Mar 11, 2022 5:49 pm

Re: How to Replace RegEx Group with uppercase?

Postby Luuk » Sat Apr 30, 2022 6:46 am

Many apologies but still we are very busy with moving, so I'm not having the time to answer everything yet.
And also its hard to make verbose, because I'm often depending on the translators to understand everything.

There is many different ways to conduct "& lowercase" ===> "& Uppercase" but to answer, you can conduct both ways.
I'm really was just trying to make it easy by first posting like...
newName = name.replace(/& ([a-z])/g, function(x) {return x.toUpperCase()})

I should have posted...
newName = name.replace(/& ([a-z])/g, function($0,$1) {return '& ' + $1.toUpperCase()})
This because functions always try to conduct against $0 unless you 1st declare it, and then omit it in the replacement.
The $0 says everything matched by /& ([a-z])/ so we forbid using $0 inside the replacement, and just use '& ' with $1.
(otherwise, it adds '& ' to every $0, even though you're saying $1)

The reason it works in the first post using (x) is because its really conducting against $0.
But since $0 is "& lowercase", the "& " cannot be uppercased, so its not to be a problem.
But if you change the "& " ==> "e ", then you can see how "e" would also get uppercased!

If you like to make a separate function like in your first example, you can make it like...
function myUpper($0,$1) {return '& ' + $1.toUpperCase()}
newName = name.replace(/& ([a-z])/g, myUpper)


Also, the replacements must either be text/variable or some function, but can never be both...
Bad: newName = name.replace(/& ([a-z])/g, '& ' + function($0,$1) {return $1.toUpperCase()})
Good:newName = name.replace(/& ([a-z])/g, function($0,$1) {return '& ' + $1.toUpperCase()})

Its unfortunate, but once you start typing 'text' in the replacement, .replace() thinks that everything else will also be text!
If any free-users like to conduct the same, then RegEx(1) can use a checkmark in "v2" with a "Match" and "Replace" like...
(& [a-z])/g
\U$1
Luuk
 
Posts: 690
Joined: Fri Feb 21, 2020 10:58 pm

Re: How to Replace RegEx Group with uppercase?

Postby Admin » Sun May 01, 2022 2:38 am

Great to have you back on the user's forum Luuk :) 8) 8)
Your help is always fantastic, thanks!
Admin
Site Admin
 
Posts: 2343
Joined: Tue Mar 08, 2005 8:39 pm


Return to Javascript Renaming