我想要的东西,以取代所有的前导空格的字符串中的正则表达式替换所有前导空格的东西
var str = ' testing 1 2 3 ',
regex = /^\s*/,
newStr = str.replace(regex, '.');
document.write(newStr)
我想要得到这样的结果:
'.....testing 1 2 3 '
有什么我失踪了吗?
我想要的东西,以取代所有的前导空格的字符串中的正则表达式替换所有前导空格的东西
var str = ' testing 1 2 3 ',
regex = /^\s*/,
newStr = str.replace(regex, '.');
document.write(newStr)
我想要得到这样的结果:
'.....testing 1 2 3 '
有什么我失踪了吗?
尝试这种情况:
var s = " a b c";
print(s.replace(/^\s+/, function(m){ return m.replace(/\s/g, '.');}));
它打印:
...a b c
测试在Ideone:http://ideone.com/v1faQ
什么:
/^([ ]+)/
我不知道\s
的伎俩,而一个普通的应该能够处理这个!
替代(忽略strnigs瓦特/没有非空间)
var newStr = "";
newStr = (newStr = Array(str.search(/[^\s]/) + 1).join(".")) + str.substr(newStr.length);
这只提供 '.testing 1 2 3'。 一期。 – Miriam
哎呀,对不起,我需要用这个预见,有一刻=) – elslooo
[Deson't work](http://jsfiddle.net/FT4rP/1/) – qwertymk