正则表达式 大写转小写

发布于 2024-08-26 18:42:26 字数 199 浏览 6 评论 0原文

是否可以将正则表达式模式匹配转换为小写?

var pattern:RegExp;
var str:String = "HI guys";
pattern = /([A-Z]+)/g;
str = str.replace(pattern, thisShouldBeLowerCase);

输出应如下所示:“嗨,伙计们”

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"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

和影子一齐双人舞 2024-09-02 18:42:26

您可以执行类似的操作,但将模式替换为您需要的模式:

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";

< code>arguments 变量是引用函数参数的内部变量。希望有帮助,

兰斯

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

述情 2024-09-02 18:42:26
var html = '<HTML><HEAD><BODY>TEST</BODY></HEAD></HTML>';
var regex = /<([^>]*)>/g;
html = html.replace(regex, function(x) { return x.toLowerCase() });
alert(html);
var html = '<HTML><HEAD><BODY>TEST</BODY></HEAD></HTML>';
var regex = /<([^>]*)>/g;
html = html.replace(regex, function(x) { return x.toLowerCase() });
alert(html);
没有你我更好 2024-09-02 18:42:26

小写

s/[A-Z]/\l&/g

和大写

s/[a-z]/\u&/g

to lowercase

s/[A-Z]/\l&/g

and to uppercase

s/[a-z]/\u&/g
在你怀里撒娇 2024-09-02 18:42:26

在 ActionSctipn 3 中将所有大写英文字母更改为小写

var pattern:RegExp = /[A-Z]/g;
contentstr = contentstr.replace(pattern,function(a:String,b:int,c:String):String { return a.toLowerCase() } );

Change all upper english letters to lower in ActionSctipn 3

var pattern:RegExp = /[A-Z]/g;
contentstr = contentstr.replace(pattern,function(a:String,b:int,c:String):String { return a.toLowerCase() } );
暮凉 2024-09-02 18:42:26

不,使用正则表达式是不可能的。您只能将 A 替换为 a,将 B 替换为 b,等等。不能一次全部替换。

为什么不简单地使用 toLowerCase()

No, that is not possible using regex. You can only replace A with a, B with b, etc. Not all at once.

Why not simply use toLowerCase()?

春风十里 2024-09-02 18:42:26

您可以使用函数作为 string.replace 的第二个参数,

在您的情况下您可以使用

var pattern:RegExp;
var str:String = "HI guys";
pattern = /([A-Z]+)/g;
str = str.replace(pattern, function(search, match1) { 
   return match1.toLowerCase() }
);

此处阅读更多信息
Javascript:如何传递找到的字符串。替换值以发挥作用?

you can use a function as the second argument for string.replace

in your case you could use

var pattern:RegExp;
var str:String = "HI guys";
pattern = /([A-Z]+)/g;
str = str.replace(pattern, function(search, match1) { 
   return match1.toLowerCase() }
);

read more about it here
Javascript: how to pass found string.replace value to function?

思慕 2024-09-02 18:42:26

如果要将完整字符串转换为小写,请在 javascript 中使用 .toLowerCase().toUpperCase()

如果您想将字符串中的特定字母替换为小写字母,那么 Regex 会更好。

If you want to convert complete string to lowercase then use .toLowerCase() or .toUpperCase() in javascript.

If you want to replace a particular letter with lowercase in a String then Regex is better.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文