by 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