jquery跨越第一个空间
我有三个单词我想在第一个空格之后添加内容
hello world
成为hello world
和
hello world again
成为hello world again
任何帮助将是欣赏
在这里编辑它
$( "#main-nav li a" ).each(function( index ) { $(this).html( $(this).text().replace(/([\S]*)\s(.*)/, "$1 $2") ); });
你可以用一些正则表达式来做到这一点:
text = text.replace(/([\S]*)\s(.*)/, "$1 $2");
如果text
是hello world
,则上面的代码会将其转换为hello world
也许更容易理解(对我来说肯定),
- 在空格字符上使用find方法,在字符串中获取第一个位置。
- 获取第一个子字符串,从索引0到特定位置。
- 从特定位置获取子串到string.length并包装它们
- 再次合并两个字符串。