为什么这个正则表达式不适用于德语单词? [英] Why this regex is not working for german words?
问题描述
我试图用单词打破下面的句子并用span包装它们。
I am trying to break the following sentence in words and wrap them in span.
<p class="german_p big">Das ist ein schönes Armband</p>
我跟着这个:
如何使用JavaScript在光标下获取单词?
$('p').each(function() {
var $this = $(this);
$this.html($this.text().replace(/\b(\w+)\b/g, "<span>$1</span>"));
});
我面临的唯一问题是,在包含span中的单词之后,生成的html如下所示:
The only problem i am facing is, after wrapping the words in span the resultant html is like this:
<p class="german_p big"><span>Das</span> <span>ist</span> <span>ein</span> <span>sch</span>ö<span>nes</span> <span>Armband</span>.</p>
因此,schönes分为三个单词sch,ö和nes。为什么会这样?什么是正确的正则表达式?
so, schönes is broken into three words sch, ö and nes. why this is happening? What could be the correct regex for this?
推荐答案
\w
仅匹配AZ,az,0-9 ,和_(下划线)。
\w
only matches A-Z, a-z, 0-9, and _ (underscore).
您可以使用类似 \S +
的内容来匹配所有非空格字符,包括像ö这样的非ASCII字符。这可能会也可能不会起作用,具体取决于字符串的其余部分的格式。
You could use something like \S+
to match all non-space characters, including non-ASCII characters like ö. This might or might not work depending on how the rest of your string is formatted.
参考: http://www.javascriptkit.com/javatutors/redev2.shtml
这篇关于为什么这个正则表达式不适用于德语单词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!