Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

regex uppercase to lowercase

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"

like image 768
luccio Avatar asked Mar 27 '10 14:03

luccio


People also ask

How do you lowercase in regular expressions?

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.

How can I tell if regex is uppercase or 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.

How do you capitalize in regex?

matches any character. \U converts to uppercase.

What does AZ regex mean?

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.


2 Answers

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

like image 93
Lance Avatar answered Oct 29 '22 02:10

Lance


var html = '<HTML><HEAD><BODY>TEST</BODY></HEAD></HTML>';
var regex = /<([^>]*)>/g;
html = html.replace(regex, function(x) { return x.toLowerCase() });
alert(html);
like image 45
ketki Avatar answered Oct 29 '22 02:10

ketki