我需要用jQuery计算字符串左边的空格.
例如:
String: " Hello how are you? " will have 4 white spaces at its left!
Run Code Online (Sandbox Code Playgroud)
我怎么能用jQuery得到它?
谢谢.
在普通的旧JavaScript中使用regexp:
var spacesOnLeft = myStr.match(/^ */)[0].length
Run Code Online (Sandbox Code Playgroud)
没有涉及循环.:)
这是普通的旧javascript可行的东西.
function countLeftBlanks(arg) {
var i = 0;
while (i < arg.length && arg[i] === ' ') {
i++;
}
return i;
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1697 次 |
最近记录: |