最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

text - Javascript: Add Break after each 100 characters - Stack Overflow

programmeradmin3浏览0评论

If I write to long text for example this:

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

The text is going out of the page, I have an idea to fix it, after every 100 characters I could make a <br /> tag. But I don't know how to do it.

Thanks for any help!

If I write to long text for example this:

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

The text is going out of the page, I have an idea to fix it, after every 100 characters I could make a <br /> tag. But I don't know how to do it.

Thanks for any help!

Share Improve this question asked Jul 7, 2010 at 10:57 Adam HalaszAdam Halasz 58.4k67 gold badges153 silver badges216 bronze badges 6
  • 1 You will find in reality this doesn't really cause a major problem. Creating a long text string like that is usually only something that you would do in a testing environment. – Andrew Commented Jul 7, 2010 at 11:01
  • 1 dup: stackoverflow./questions/1772941/… – Haim Evgi Commented Jul 7, 2010 at 11:01
  • 1 This is not text. Normal text contains words (letters with spaces) and it breaks by your browser automatically. – GOsha Commented Jul 7, 2010 at 11:03
  • 4 Just to answer your question str = str.replace(/(.{100})/g, "$1<br/>"); – Amarghosh Commented Jul 7, 2010 at 11:04
  • 1 without inserting </br> s.replace(/([^\n]{100})/g, '$1\n'); – DomreiRoam Commented Jul 7, 2010 at 11:46
 |  Show 1 more ment

2 Answers 2

Reset to default 7

Just use the following CSS property on the element that you wish to force wrapping in:

word-wrap: break-word;

No need for any JavaScript!

If you really want to use JavaScript (for concept; CSS is better for accessibility and ease) then go ahead.

Oh, and another thing, if you're using a font that isn't monospace (but rather, proportional), cutting off at 100 characters could be ineffective. One line could have 100 'i' characters, with the next having another hundred 'm' chars, which are very different in size.

Oh, and another thing, you can't just apply a regex replace on the innerHTML unless it's all text. If there could possibly be other elements there, you must actually loop through the nodes, applying the technique to only text nodes.

Oh, and another thing, don't bother. Too many problems doing it with javascript.

You can try the following code

str = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa';

len = str.length;
loop = len / 100;
document.write(loop);
document.write('<br>');

for(i=0; i<=loop; i++){
  document.write( str.slice( i*100, (i*100)+100) );
  document.write('<br>');
}
发布评论

评论列表(0)

  1. 暂无评论