1

Possible Duplicate:
How do I trim a string in javascript?

Using JavaScript without any frameworks how would you trim line breaks? Trim as being defined by PHP, removing the leading and ending line breaks while preserving line breaks after the first non-white-space character and until though not beyond the last non-white-space character.

By default most people will want to know how to remove both leading/ending line breaks as well as white-space spaces too though some may want to retain the white-space spaces while trimming just the white-space line-breaks. It also generally helps to see two working examples and how they relate when they both work for people learning code, so I'm looking for trimming both white-space line-breaks and white-space spaces.

It'd be good also to see how to trim just the leading/ending line breaks while preserving white-space spaces (which may or may not be included in the main answer).

halfer
  • 19,824
  • 17
  • 99
  • 186
John
  • 1
  • 13
  • 98
  • 177
  • 3
    Um, https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/Trim ? – j08691 Jul 27 '12 at 16:17
  • 1
    PHP's `trim` also removes white-spaces: http://php.net/manual/en/function.trim.php. Also I'm pretty sure [this question](http://stackoverflow.com/questions/498970/how-do-i-trim-a-string-in-javascript) covers it sufficiently. The step from trimming only line breaks is small, people should be able to adapt solutions to their needs. – Felix Kling Jul 27 '12 at 16:18
  • @FelixKling No, that question is far too ambiguous. – John Jul 27 '12 at 16:31
  • @Utkanos If you read the question you would know my need for it was negated. – John Jul 27 '12 at 16:33
  • @Utkanos Not hostile and not intended to be interpreted that way. It *is* ambiguously implied though not explicitly that since an unrelated issue was resolved that I did not have to make any attempts. – John Jul 27 '12 at 16:40
  • @j08691 Great though not listed in ECMAScript 5.1 or DOM4 (would very likely be in the ECMAScript specification). Like serialization it is very useful thus likely to be widely implemented though I haven't bothered to look at other browsers. – John Jul 27 '12 at 16:43
  • It is in the specification: http://ecma-international.org/ecma-262/5.1/#sec-15.5.4.20 – Felix Kling Jul 27 '12 at 16:45
  • @FelixKling Ah, I searched for "trim" and encountered numerous instances that were unrelated, changed it to "trim(" and found nothing. I'll contact the editor to correct it. Thanks for pointing it out. – John Jul 27 '12 at 16:47
  • @John BTW, *don't* add the tag to the beginning of question titles. That's not how we do things here. – casperOne Jul 27 '12 at 17:03
  • @casperOne ...and that is why I did not find that question, it was poorly presented even if on-topic. – John Jul 27 '12 at 17:08
  • 1
    @John Perhaps, but we don't use tags into the title unless it makes sense naturally to do so. That said, please refrain from doing so in the future. Also, **do not** edit out the duplicate notification. We absolutely **do not** allow that here. – casperOne Jul 27 '12 at 17:10

2 Answers2

2

I think this works:

string.replace(/^\s+|\s+$/g,"");
meiamsome
  • 2,876
  • 1
  • 17
  • 19
  • Had to go to Meta to find out how to see seconds posted (mouseover (mins/hours/days ago). Yours was first and does both, thanks! – John Jul 27 '12 at 16:45
  • @John: Since you accepted this answer, which was also given here: http://stackoverflow.com/questions/498970/how-do-i-trim-a-string-in-javascript your question can closed as duplicate. – Felix Kling Jul 27 '12 at 16:49
1

trim in general can be defined as .replace(/^\s+|\s+$/g,''), but since you want only vertical whitespace you should use .replace(/^[\r\n]+|[\r\n]+$/g,'').

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592