1

I'm using the RegExp from this SO to shorten string without cutting words. Unfortunately this dont work when there are line breaks in the tested string. Is there a way to shorten string without cutting words and also after at the first line break.

"this is a longish string of \n\n test".replace(/^(.{11}[^\s]*).*/, "$1"); 
//Expected output: "this is a longish"
//Actual output: "this is a longish test"
Community
  • 1
  • 1
Andreas Köberle
  • 106,652
  • 57
  • 273
  • 297

1 Answers1

3

Have you tried inserting another '.replace()' before the one you've got?

Example:

var longString = "this is a longish<br> string of test";
longString = longString.replace(/\n/g, "").replace(/^(.{11}[^\s]*).*/, "$1");

Perhaps something to that effect will help? You may need to play with the formatting a little, as I'm not sure how you want the string to actually end up looking, or how the <br> tags in your page are formatted, etc.

Good luck!

quotemyname
  • 100
  • 1
  • 2
  • 7