1

我正在寻找一个脚本来在多个单词之后放置一个换行符。我在stackoverflow中找到了这个:Applying a style to and insert a line break after the first word in a link

所以,我稍微改变一下:

var el = $('.wtt');   //where wtt is the class of the elements that you want to add the <br />
var text = obj.html();
var parts = text.split(' ');
//number 7 represents 8 words
$('.wtt').html($('.wtt').html().replace(parts[7],parts[7]+'<br />'));
4

1 回答 1

0

以下将用 a 替换字符串中的第 8 个空格<br>

var text = 'hello there purple giraffe, how are you doing today.';
alert(text.replace(/^((\S+\s+){7}\S+)\s+/, '$1<br>'));

\S+匹配一个或多个非空格字符。\s+匹配一个或多个空格字符。

于 2011-09-24T17:03:25.020 回答