I was searching for a script to put a line break after a number of words. I开发者_开发问答 found this here in stackoverflow: Applying a style to and inserting a line break after the first word in a link
So, i change it a little:
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 />'));
The following would replace the 8th space in a string with a <br>
var text = 'hello there purple giraffe, how are you doing today.';
alert(text.replace(/^((\S+\s+){7}\S+)\s+/, '$1<br>'));
\S+
matches one or more non-space character. \s+
matches one or more space character.
精彩评论