To trim for extra white spaces before and after the string, you can use trim function of the jQuery.
<script type="text/javascript">
var v = $.trim(" There are extra spacces before and after this sentences. ");
alert(v);
</script>
This will trim the extra white spaces before and after the sentence and alert the result string.