2015/04/23

[CoffeeScript]replaceメソッドの評価について

下のようにCoffeeScript組んだらエラーが発生した。

text = 'hoge foo'
text = text.replace / /gi, ''
で、文字列を直接しているのがいけないのかなーっと思って下のように書き直してもダメでした。
key = ' '
text = 'hoge foo'
text = text.replace /#{key}/gi, ''
結果的にこれで落ち着いた。
key = ' '
text = 'hoge foo'
text = text.replace ///#{key}///gi, ''

2 件のコメント:

  1. 下記でどうでしょう?
    text = 'hoge foo'
    text = text.replace (/ /gi) , ''

    返信削除
  2. 試してみまっす!!

    返信削除