Possible Duplicate:
How do I trim a string in javascript?
By using replace method in javascript I am trying to remove the empty space between the start and the end of a string:
Here my code:
Any idea how should I achive the result?
input -> " first second ".replace(/[^\s|\s$]/g, ''); // " "
output -> "first second"
This is called trimming.
You need parentheses instead of brackets in the regular expression, and also a multiplier on the white space specifier to match multiple spaces:
Demo: http://jsfiddle.net/Guffa/N7xxt/