Why not try this?
html:
<p>
a b c
</p>
js:
$("p").text().trim();
JavaScript does have a native .trim()
method.
var name = " John Smith ";
name = name.trim();
console.log(name); // "John Smith"
The trim() method removes whitespace from both ends of a string. Whitespace in this context is all the whitespace characters (space, tab, no-break space, etc.) and all the line terminator characters (LF, CR, etc.).
or just use $.trim(str)
Source: Stackoverflow.com