Andreas Köberle
Andreas Köberle

Reputation: 111062

How to detect line break with this RegExp

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"

Upvotes: 1

Views: 248

Answers (1)

quotemyname
quotemyname

Reputation: 100

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!

Upvotes: 3

Related Questions