Reputation: 7705
I want to sanitize poetry text, below is the sample: (used code
tag to better see hidden characters):
მეორდება ისტორია,
დღევანდელს ჰგავს გარდასული,
რახან ბევრი გვიცხოვრია,
ნუთუ დგება დასსარული?!
არა! არ თქვა დავბერდითო, ნუ მაჯერებ, რაც არ მჯერა, არ მწამს სიტყვა ავბედითი, რომ ჩამძახონ ათასჯერაც!
I tried with:
function sanitize(txt)
{
txt = txt.replace(/\s+\n/g, "\n");
return txt;
}
It works but also removes new lines between paragraphs. I just want to remove extra white space from the end and sometimes from the start from each line and leave new lines as much as presented
I know it'd be easy to solve but I'm stuck
Thanks
Upvotes: 1
Views: 71
Reputation: 67998
^[ ]+|[ ]+$
Try this.Replace by empty string
.See demo.
http://regex101.com/r/oE6jJ1/48
var re = /^[ ]+|[ ]+$/igm;
var str = 'მეორდება ისტორია,\nდღევანდელს ჰგავს გარდასული, რახან ბევრი გვიცხოვრია, ნუთუ დგება დასსარული?! \n\n არა! არ თქვა დავბერდითო, ნუ მაჯერებ, რაც არ მჯერა, არ მწამს სიტყვა ავბედითი, რომ ჩამძახონ ათასჯერაც! ';
var subst = '';
var result = str.replace(re, subst);
Upvotes: 1
Reputation: 174874
\s
matches spaces including line breaks.
txt.replace(/^ +| +$/gm, "");
This removes one or more horizontal spaces (except tabs) which are present at the start or at the end of a line.
Upvotes: 1