我正在寻找一个脚本,在一些单词之后放置一个换行符。我在stackoverflow中找到了这个:Applying a style to and inserting 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 />'));
答案 0 :(得分:0)
以下内容将使用<br>
var text = 'hello there purple giraffe, how are you doing today.';
alert(text.replace(/^((\S+\s+){7}\S+)\s+/, '$1<br>'));
\S+
匹配一个或多个非空格字符。 \s+
匹配一个或多个空格字符。