jquery跨越第一个空间 [英] jquery span after first space

查看:81
本文介绍了jquery跨越第一个空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个单词我想在第一个空格后添加内容,所以

I have three words I would like to add a span around the content after the first space so

hello world

变为:

hello <span>world</span>

和:

hello world again

成为:

hello <span>world again</span>

这是我的JavaScript代码:

Here is my JavaScript code:

 $( "#main-nav li a" ).each(function( index ) {
   $(this).html( $(this).text().replace(/([\S]*)\s(.*)/, "$1 <span>$2</span>") );
}); 


推荐答案

你可以用一些正则表达式来实现:

You can do it with some regex:

text = text.replace(/([\S]*)\s(.*)/, "$1 <span>$2</span>");

如果 text hello world ,上面的代码会将其转换为 hello< span> world< / span>

If text is hello world, the above code will convert it to hello <span>world</span>

这篇关于jquery跨越第一个空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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