-4

How can I delete spaces before only text:

"          with spaces between" 

I need:

"some text with spaces between"

All I have found it's text.replace(/\s/g, '') but it doesn't work well:

"sometextwithspacesbetween"

and indexOf(' ') + 25 also isn't good solve, because I have a different text and number of spaces before it.

If it possible, help me please

Marc B
  • 356,200
  • 43
  • 426
  • 500
supermishboy
  • 99
  • 11
  • 4
    [trim()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/Trim) – Teemu Aug 10 '15 at 16:45
  • http://stackoverflow.com/questions/10032024/how-to-remove-leading-and-trailing-white-spaces-from-a-given-html-string Possible Duplicate? – jonny Aug 10 '15 at 16:46
  • 2
    Question shows no effort to research. Not hard to find lots of answers for this – charlietfl Aug 10 '15 at 16:51
  • And what do you expect about any extra spaces after text? Should it be removed or kept? – A. Wolff Aug 10 '15 at 16:52

2 Answers2

1

Set the reg exp to look for the beginning of the string

text.replace(/^\s+/, '')
epascarello
  • 204,599
  • 20
  • 195
  • 236
0

You can use trim() in javascript and jQuery.trim(string) in jQuery. Both of them remove the whitespace from the beginning and end of a string.

var str = "                         some text with spaces between";
console.log(str);

console.log(str.trim());
console.log(jQuery.trim(str));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
Pranav C Balan
  • 113,687
  • 23
  • 165
  • 188