0

次のコードがあります。

ttt = 'hello---,,..there';
tt2 = strip(ttt);
alert(tt2);

function strip(str){
  return str.replace(/[^a-zA-Z0-9 ,.-_]/g, '');
}

アラートが与えるhello,,..there

hello---,,..thereハイフンを含むすべての文字が置換関数の例外として指定されているため、それが返されることを期待しています。

私は何を間違っていますか?

ありがとう。

4

1 に答える 1

0

ハイフンをエスケープします。

'hello---,,..there'.replace(/[^a-zA-Z0-9 ,.\-_]/g, ''); // => "hello---,,..there"
于 2012-08-11T13:41:40.270 に答える