Erdem Güngör
Erdem Güngör

Reputation: 877

javascript remove whitespace carriage return

how can I remove whitespace and carriage return from a String. I got something like this..

exampleName↵

but need just the text and not the symbol at the end.

Upvotes: 0

Views: 2637

Answers (3)

urban
urban

Reputation: 5682

What about trim method?

var str = "       Hello World!        ";
alert(str.trim());

Edit based on the comments: I am not sure what character '↵' is... trim() is supposed to remove white-space chars including new-line and carriage-return... so what about the following

var a="exampleName↵";
a.replace(/↵/g, "")
alert(a)

Fiddle: https://jsfiddle.net/dbth6tx8/4/

Upvotes: 5

Panos Bariamis
Panos Bariamis

Reputation: 4653

try this

var s = '   Your string with newline or/and carriage return   ';
s = s.replace(/(?:\r\n|\n|\r)/g, '').replace(/^\s+|\s+$|\s+(?=\s)/g, '');
  • \r\n for the DOS\Windows world
  • \r for the pre-OSX Mac world
  • \n for the Unix and Unix-like world

Upvotes: 1

Harshada Chavan
Harshada Chavan

Reputation: 536

var a="exampleName↵"; 

var mystring = a.replace(/[^\w\s]/gi, '');

alert(mystring);

Upvotes: 0

Related Questions