You need to translate all Latin characters in the string to upper case.
This code does not work:

"Siemens system corp.".replace(/([Az])/g, "$1".toUpperCase()) 
  • whence such a strange interval in the expression? - Grundy
  • You forgot to use the arrow functions, and the rest of your code is correct :) "Siemens system corp.".replace(/([Az])/g, $1 => $1.toUpperCase())

1 answer 1

The .replace() method takes a second parameter function. You can do exactly what you want in it:

 console.log( "АбВ Siemens system corp. эЮя".replace(/[az]+/g, function (match) { return match.toUpperCase()} ) ); 

Note that memorizing a match in a regular expression is not required. In addition, it makes no sense to look for letters that are already in upper case.
Well, the line in the form in which you brought it, in general, can be processed through "Siemens system corp.".toUpperCase() , this is so, by the way.

And the documentation even says why your method does not work:

Since we want to perform additional transformations of the matching result before the final substitution is used, we must use the function. This forces us to compute the mapping before using the toLowerCase() method. If we tried to use a mapping without a function, the toLowerCase() method toLowerCase() not work:

var newString = propertyName.replace(/[AZ]/g, '-' + '$&'.toLowerCase());

This happens because at first the part '$&'.toLowerCase() computed into a string literal (the result is still equal to '$&' ), and only then its characters are used as a template.

  • You can slightly improve performance by replacing the regular expression with [az]+ , so there will be fewer calls to the replay function. - ReinRaus
  • @ReinRaus, you are right, corrected the code in the answer. - Alexey Ukolov