本文介绍了用jQuery在字符串中的第一个单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试找出可以帮助我将字符串中的第一个单词包装到span中的函数.

I'm trying to figure out function that will help me wrap first word in string into span.

但不幸的是没有运气.

有人可以帮我吗?

非常感谢

Dom

推荐答案

一种方法:

$('something').each(function(){
     var me = $(this);
     me.html(me.html().replace(/^(\w+)/, '<span>$1</span>'));
});

基本上,对于每个匹配项(something),您都将第一个单词替换为包裹在<span>标签中的自身($1).字符类\w匹配字母,数字和下划线(因此定义单词"是什么-您可能需要另一个定义).

Basically, for each match (something) you replace the first word with itself ($1) wrapped in <span> tags. The character class \w matches letters, digits, and underscores (thus defining what a "word" is - you may need another definition).

这篇关于用jQuery在字符串中的第一个单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 17:48
查看更多