0
var firefoxIngs = myText.replace(/ /g," ");

これは私の文字列です:

"h  a"//it has two empty spaces between h and a

コードの後は次のようになります。

"h a"//it has one empty spaces between h and a

なりたい

"h  a"

なぜこれが起こっているのか、それを修正する方法は?

4

2 に答える 2

3

期待どおりに動作します

console.log("h  a".replace(/ /g," "));

文字列では、1 つのスペースが欠落している可能性があります。

結果:

h  a

ライブデモを参照

于 2012-11-27T08:14:53.613 に答える
0

これも機能します

<script type="text/javascript">
 function validate(){
  str=document.getElementById("input").value;

str= str.replace(/\s/g, '  ');
console.log(str);
}
</script>
    <input id="input" type="text"/>
<input type="button" onclick="validate()" />
于 2012-11-27T08:19:19.740 に答える