alert(' string to be trimmed '.replace(/(^\s*)|(\s*)$/g, ''))
var str = " sdfffffffff " str=str.replace(/^\s*/,'').replace(/\s*$/,''); alert(">"+str+"<")
var strWithSpace = ' Текст внутри пробелов '; alert(strWithSpace.replace(/^\s*(.*)\s*$/, '$1'));