Monday, May 13, 2024
 Popular · Latest · Hot · Upcoming
137
rated 0 times [  140] [ 3]  / answers: 1 / hits: 17771  / 10 Years ago, sat, september 13, 2014, 12:00:00

What is the best way to count how many spaces before the fist character of a string?



str0 = 'nospaces even with other spaces still bring back zero';
str1 = ' onespace do not care about other spaces';
str2 = ' twospaces';

More From » regex

 Answers
0

Use String.prototype.search



'    foo'.search(/S/);  // 4, index of first non whitespace char


EDIT:
You can search for Non whitespace characters, OR end of input to avoid checking for -1.



'    '.search(/S|$/)

[#69468] Thursday, September 11, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
andrea

Total Points: 445
Total Questions: 95
Total Answers: 103

Location: Tonga
Member since Tue, Nov 30, 2021
3 Years ago
;