I’m trying to convert XML tags to uppercase, while preserving the case of attributes and text. So for example
<Mytag Category="Parent">Value1</Mytag>
Becomes
<MYTAG Category="Parent">Value1</MYTAG>
I have a regex which matches the XML tags correctly, but the upperCase function does not seem to be working.
myXmlElement.replace(/<(\/)*([a-zA-Z_0-9]+)([^>]*)>/g,"<$1" + "$2".toUpperCase() + "$3>")
I’ve also tried using String.prototype.toUpperCase.apply("$2"), as well as passing a function as the replace argument
myXmlElement.replace(/<[\/]*([a-zA-Z_0-9]+)[^>]*>/g,
function($1,$2,$3){return <$1 + $2.toUpperCase() + $3>})
But this doesn’t work, as $1,$2,$3 appear to refer to the entire matching elements ($1 = , $2 = )
I’m sure there is something trivial I am overlooking here, can anybody help out?
If you want to match the characters before and after your tag name, the need to be put into matching braces within the pattern:
The replacement function will take the
fullmatching expression as first argument. That’s why you simply may ignore it.After that any matching brace of your pattern will be passed as a parameter.