字符串jQuery留下的空格数量

eut*_*her 1 javascript jquery

我需要用jQuery计算字符串左边的空格.

例如:

 String: "    Hello how are you? "  will have 4 white spaces at its left!
Run Code Online (Sandbox Code Playgroud)

我怎么能用jQuery得到它?

谢谢.

dra*_*gon 8

在普通的旧JavaScript中使用regexp:

var spacesOnLeft = myStr.match(/^ */)[0].length
Run Code Online (Sandbox Code Playgroud)

没有涉及循环.:)


Jar*_*Par 5

这是普通的旧javascript可行的东西.

function countLeftBlanks(arg) {
  var i = 0;
  while (i < arg.length && arg[i] === ' ') {
    i++;
  }
  return i;
}
Run Code Online (Sandbox Code Playgroud)