javascript string.replace在德国Umlaute失败 [英] javascript string.replace fails at German Umlaute

查看:62
本文介绍了javascript string.replace在德国Umlaute失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个小的Javascript / Jquery函数将元素注入到某些文本中,如果还没有这样的元素。

I am using a small Javascript/Jquery function to inject a element into some text, if there is not already such an element.

代码如下所示:

$(document).ready(function () {
$(function () {
    $(".replaceText").html(function (i, text) {
        if ($('.replaceText > span.dbOrange').length) {
            return;
        }
        else {
            return text.replace(/\w+\s\w+/, function (match) {
                return '<span class="dbOrange">' + match + '</span>';
            });
        }
    });
});

});

10个案例中有9个可以正常使用。前两个单词的样式为橙色和粗体。但是,如果第二个单词包含像äöü这样的变形符号,那么正则表达式就会破坏这个角色,看起来这个单词会在那里结束。

This works fine in 9 out of 10 cases. The first two words are styled with orange color and bold. However, if a the the second word contains an Umlaut like ä ö ü the Regex breaks at this character and it looks like the word would end there.

你能帮助我吗?更安全的解决方案?

Can you help me with a more failsafe solution?

亲切的问候

推荐答案

试试这个:

/\S+\s\S+/

\S匹配非空白字符。

\S matches non-whitespace characters.

这篇关于javascript string.replace在德国Umlaute失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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