LoginSignup
0
2

More than 5 years have passed since last update.

JavaScriptである文字列からとある文字すべてを置換するRegExp関数の使いかた

Posted at

Javascriptの中の文字列を正しくすべて置き換えるには、RegExp関数を使用します。
でも、レファレンスをみるとちょっと書き方が不親切な気が。
javascript
/ab+c/i;
new RegExp('ab+c', 'i');
new RegExp(/ab+c/, 'i');

https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/RegExp より引用

ただしくはたてに並べるのではなく、xxx.push("置換対象","置換後の文字列")の置換対象の部分にRegExpを当てはめることで、このRegExpでの正規表現の文字置き換えを実装することができる。

 for(var i = 0; i < array2.length; i++){
        var txtToPush = array2[i].innerText.replace(new RegExp("\n",'g'),"");
//        var txtToPush = .replace('\n','');
        arrayToReturn.push(txtToPush);
      }
0
2
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2