Javascript / jQuery大写首字母变量带重音符号 [英] Javascript/jQuery uppercase first letter of variable with accents

查看:91
本文介绍了Javascript / jQuery大写首字母变量带重音符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有一些文字输入的表格,可以插入一个人的名字姓氏,但我想要将每个单词的第一个字母更改为大写,我找到了这个解决方案:

I have a form with some text inputs to insert First Name, Last Name of a person, but I want to change the first letter of each word to a Uppercase and I found this solution:

 // The textboxes with this class are modified
 $(".toUpper").change(function () {
        var str = $(this).val();
        str = str.toLowerCase().replace(/\b[a-z]/g, function (letter) {
            return letter.toUpperCase();

        });
        $(this).val(str);
    });

它有效,(hEllO=>你好, whAts uP=>Whats Up)

当我尝试将此应用于带重音的单词时出现问题,例如:

The problem occurs when I try to apply this to an accented word, Example:

"gonzález" = "GonzáLez",
"pérez" = "PéRez"

在重音后再次出现大写字母。

After an accented word there is a Uppercase letter again.

如何我可以修改正则表达式以避免这个问题吗?

How can I modify the regular expression to avoid this issue?

希望你能帮助我! :)

hope you can help me!! :)

推荐答案

\ b 是一个非单词边界(即 \b 会为任何不属于 [0-9a-zA-Z _] <0的任何1个字符设置边界

\b is a non word boundary (i.e \b would make a boundary for any any character which doesn't belong to any 1 of [0-9a-zA-Z_])

所以这些带重音的单词成为你单词的边界..

So those accented word become the boundary for your word..

相反使用这个正则表达式

Instead use this regex

/(^|\s)[a-z\u00E0-\u00FC]/g

这篇关于Javascript / jQuery大写首字母变量带重音符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆