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())
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())
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, thetoLowerCase()
methodtoLowerCase()
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.
[az]+
, so there will be fewer calls to the replay function. - ReinRausSource: https://ru.stackoverflow.com/questions/550510/
All Articles
"Siemens system corp.".replace(/([Az])/g, $1 => $1.toUpperCase())