Is it possible to transform regexp pattern match to lowercase?
var pattern:RegExp;
var str:String = "HI guys";
pattern = /([A-Z]+)/g;
str = str.replace(pattern, thisShouldBeLowerCase);
Output should look like this: "hi guys"
This can be done easily using regular expressions. In a substitute command, place \U or \L before backreferences for the desired output. Everything after \U , stopping at \E or \e , is converted to uppercase. Similarly, everything after \L , stopping at \E or \e , is converted to lowercase.
Using character sets For example, the regular expression "[ A-Za-z] " specifies to match any single uppercase or lowercase letter. In the character set, a hyphen indicates a range of characters, for example [A-Z] will match any one capital letter.
matches any character. \U converts to uppercase.
The regular expression [A-Z][a-z]* matches any sequence of letters that starts with an uppercase letter and is followed by zero or more lowercase letters. The special character * after the closing square bracket specifies to match zero or more occurrences of the character set.
You can do something like this, but replace the pattern with exactly what you need:
public static function lowerCase(string:String, pattern:RegExp = null):String
{
pattern ||= /[A-Z]/g;
return string.replace(pattern, function x():String
{
return (arguments[0] as String).toLowerCase();
});
}
trace(lowerCase('HI GUYS', /[HI]/g)); // "hi GUYS";
That arguments
variable is an internal variable referencing the function parameters. Hope that helps,
Lance
var html = '<HTML><HEAD><BODY>TEST</BODY></HEAD></HTML>';
var regex = /<([^>]*)>/g;
html = html.replace(regex, function(x) { return x.toLowerCase() });
alert(html);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With