I've the following HTML string. What would be sample code in JavaScript to remove leading and trailing white spaces from this string?
<p> </p>
<div> </div>
Trimming using JavaScript<br />
<br />
<br />
<br />
all leading and trailing white spaces
<p> </p>
<div> </div>
Best Answer
See the String method
trim()
- https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/TrimEdit
As noted in other comments, it is possible to use the regex approach. The
trim
method is effectively just an alias for a regex:... this will inject the method into the native prototype for those browsers who are still swimming in the shallow end of the pool.