简体   繁体   English

Javascript正则表达式匹配特殊字符的最后一个单词

[英]Javascript regex to match last word regardless of specials characters

We use this script to alter the last word in a sentence. 我们使用此脚本来更改句子中的最后一个单词。

$div = $('.cPageHeader h2');
$div.html($div.text().replace(/(\w+?)$/, '<span class="cOrange">$1</span>'));

This works well as long as there are no special chars involved. 只要不涉及特殊字符,此方法就很好用。

As soon as we have a header like <h2>International fancy stüff</h2> the highlighting goes wrong. 一旦有了<h2>International fancy stüff</h2>这样的标题,突出显示就会出错。 Only ff will be highlighted. 仅ff将突出显示。 The same error occurs if we have one of the chars at the end of the line (!-.?) . 如果我们在行尾有一个字符(!-.?)则会发生相同的错误。

Can someone alter the script, so that the last whole word (including attached punctuation) and regardless of any accented chars, will be highlighted? 有人可以更改脚本,以使最后一个完整的单词(包括附加的标点符号)和任何重音字符突出显示吗?

I'd suggest: 我建议:

$div = $('.cPageHeader h2');
$div.html($div.text().replace(/(\S+?)$/, '<span class="cOrange">$1</span>'));

JS Fiddle demo . JS小提琴演示

This basically looks for all the non white-space characters at the end of your string, although if your string ends with white-space, there'll be no highlight (so it might be worth trimming the string first , just to be sure. 基本上,这会在字符串的末尾查找所有非空格字符,尽管如果字符串空格结尾 ,则不会出现突出显示(因此请务必首先对字符串进行修剪,以确保。

The following replicates the above, but is a little more tolerant of trailing white-space: 以下内容复制了上面的内容,但是对尾随空白的容忍度更高:

var $div = $('#demo');
$div.html($div.text().replace(/\b(\S+?)(\b|(?:\s+))$/, '<span class="cOrange">$1</span>'));

JS Fiddle demo . JS小提琴演示

This matches: 这符合:

  • \\b : a word-boundary; \\b :一个单词边界;
  • \\S+ : a sequence of one, or more, non white-space characters; \\S+ :一个或多个非空格字符的序列;
  • (\\b|(?:\\s+)) : another word-boundary or a sequence of one, or more, white-space characters. (\\b|(?:\\s+)) :另一个单词边界一个或多个空格字符的序列。

Updated once more, because the numbered-matches (the $1 ) from your original expression are apparently deprecated, or will soon be (though I cannot find a reference to back up that particular recollection, so perhaps take it with a pinch of salt), and to use a function instead: 更新一次,因为编号,匹配(在$1从原始表达式)显然过时了,或者很快会(虽然我不能找到一个参考备份特定的回忆,所以也许把它用少许盐),并改用一个函数:

var $div = $('#demo');
$div.html($div.text().replace(/\b(\S+?)(\b|(?:\s+))$/, function(a){
    return '<span class="cOrange">' + a + '</span>';
}));

JS Fiddle demo . JS小提琴演示

References: 参考文献:

This should be all you need: 这应该是您所需要的:

$div.text().replace(/(\S+)$/, '<span class="cOrange">$1</span>')

You want to include the trailing punctuation in the match anyway, so \\w+ never was the right tool for the job. 无论如何,您都希望在比赛中包含尾随标点符号,因此\\w+从来都不是正确的作业工具。 And this way you don't have to deal with making it treat non-ASCII characters like ü as word characters. 这样,您就不必处理使其将ü等非ASCII字符视为单词字符。

Just FYI, there's no point using a reluctant quantifier like \\S+? 仅供参考,使用\\S+?这样的勉强量词没有意义\\S+? , since you're matching all the way to the end of the string. ,因为您要一直匹配到字符串的末尾。 It's not incorrect in this case, just pointless. 在这种情况下这是不正确的,只是毫无意义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM