Try this construct:
re:replace(A, "\\s+", "", [global,{return,list}]).
Session Example:
Erlang R15B01 (erts-5.9.1) [source] [async-threads:0] [hipe] [kernel-poll:false] Eshell V5.9.1 (abort with ^G) 1> A = " 21\t\n ". " 21\t\n " 2> re:replace(A, "\\s+", "", [global,{return,list}]). "21"
UPDATE
The above solution will also separate the space characters inside the string (not just leading and trailing).
If you need to remove only the lead and tail, you can use something like this:
re:replace(re:replace(A, "\\s+$", "", [global,{return,list}]), "^\\s+", "", [global,{return,list}]).
Session Example:
Erlang R15B01 (erts-5.9.1) [source] [async-threads:0] [hipe] [kernel-poll:false] Eshell V5.9.1 (abort with ^G) 1> A=" \t \n 2 4 \n \t \n ". " \t \n 2 4 \n \t \n " 2> re:replace(re:replace(A, "\\s+$", "", [global,{return,list}]), "^\\s+", "", [global,{return,list}]). "2 4"