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"); 

如果texthello world ,则上面的代码会将其转换为hello world

也许更容易理解(对我来说肯定),

  1. 在空格字符上使用find方法,在字符串中获取第一个位置。
  2. 获取第一个子字符串,从索引0到特定位置。
  3. 从特定位置获取子串到string.length并包装它们
  4. 再次合并两个字符串。