admin管理员组文章数量:1023827
I want to add a span class after the first word in a string with jQuery. I am using following code:
$('h1').each(function(){
var test = $(this);
test.html( test.text().replace(/(^\w+)/,'<span>$1</span>') );
});
This will work fine with the standard characters, but it won't work with the non-English Characters.
Please see the demo, it should select the whole word "Bài", but it only selects "B". How this can be fixed?
Demo: /
Thanks.
I want to add a span class after the first word in a string with jQuery. I am using following code:
$('h1').each(function(){
var test = $(this);
test.html( test.text().replace(/(^\w+)/,'<span>$1</span>') );
});
This will work fine with the standard characters, but it won't work with the non-English Characters.
Please see the demo, it should select the whole word "Bài", but it only selects "B". How this can be fixed?
Demo: http://jsfiddle/hez4ohpc/
Thanks.
Share Improve this question edited Aug 25, 2014 at 7:50 Denys Séguret 383k90 gold badges811 silver badges777 bronze badges asked Aug 25, 2014 at 7:43 user1355300user1355300 4,99718 gold badges51 silver badges73 bronze badges4 Answers
Reset to default 6Support for Unicode in JavaScript regular expressions is currently very poor (this will be much better in ES6). In such a case your best option would probably be to use a more plete regular expression solution. The best I know is the XRegExp library.
The second best option would be to try to define what doesn't make a word :
.replace(/([^\s\.,\:;!?]+)/,'<span>$1</span>')
but that's dangerous and you'll expose you to surprises later when you'll realize what caracter you forgot.
Try this:
$('h1').each(function(){
var test = $(this);
test.html( test.text().replace(/(^[^\s]+ )/,'<span>$1</span>') );
});
Maybe that?
$('h1').each(function(){
var test = $(this);
test.html( test.text().replace(/(^[^\s,\.\:\;\"]+)/,'<span>$1</span>') );
});
Here is an alternative Solution to your Question, with which you have no need to escape or .replace anything.
$('h1').each(function() {
var html = $(this).html();
var word = html .substr(0, html.indexOf(" "));
var rest = html .substr(html.indexOf(" "));
$(this).html(rest).prepend($("<span/>").html(word).addClass("em"));
});
JSFiddle
EDIT: As discussed in the ments, the downside of this solution is, that it will not work, if the first word is not followed by a space.
I want to add a span class after the first word in a string with jQuery. I am using following code:
$('h1').each(function(){
var test = $(this);
test.html( test.text().replace(/(^\w+)/,'<span>$1</span>') );
});
This will work fine with the standard characters, but it won't work with the non-English Characters.
Please see the demo, it should select the whole word "Bài", but it only selects "B". How this can be fixed?
Demo: /
Thanks.
I want to add a span class after the first word in a string with jQuery. I am using following code:
$('h1').each(function(){
var test = $(this);
test.html( test.text().replace(/(^\w+)/,'<span>$1</span>') );
});
This will work fine with the standard characters, but it won't work with the non-English Characters.
Please see the demo, it should select the whole word "Bài", but it only selects "B". How this can be fixed?
Demo: http://jsfiddle/hez4ohpc/
Thanks.
Share Improve this question edited Aug 25, 2014 at 7:50 Denys Séguret 383k90 gold badges811 silver badges777 bronze badges asked Aug 25, 2014 at 7:43 user1355300user1355300 4,99718 gold badges51 silver badges73 bronze badges4 Answers
Reset to default 6Support for Unicode in JavaScript regular expressions is currently very poor (this will be much better in ES6). In such a case your best option would probably be to use a more plete regular expression solution. The best I know is the XRegExp library.
The second best option would be to try to define what doesn't make a word :
.replace(/([^\s\.,\:;!?]+)/,'<span>$1</span>')
but that's dangerous and you'll expose you to surprises later when you'll realize what caracter you forgot.
Try this:
$('h1').each(function(){
var test = $(this);
test.html( test.text().replace(/(^[^\s]+ )/,'<span>$1</span>') );
});
Maybe that?
$('h1').each(function(){
var test = $(this);
test.html( test.text().replace(/(^[^\s,\.\:\;\"]+)/,'<span>$1</span>') );
});
Here is an alternative Solution to your Question, with which you have no need to escape or .replace anything.
$('h1').each(function() {
var html = $(this).html();
var word = html .substr(0, html.indexOf(" "));
var rest = html .substr(html.indexOf(" "));
$(this).html(rest).prepend($("<span/>").html(word).addClass("em"));
});
JSFiddle
EDIT: As discussed in the ments, the downside of this solution is, that it will not work, if the first word is not followed by a space.
本文标签: javascriptSelect first word in string with jQueryStack Overflow
版权声明:本文标题:javascript - Select first word in string with jQuery - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745600129a2158406.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论