LoginSignup
4

More than 5 years have passed since last update.

JavaScriptで複数行文字列

Posted at

Firefoxがver.17からFunction.toStringがコメントを含めたコードを返すようになっていたので、
こんな書き方もできるようになったのかなぁと。

var text = function() {/*
<html>
    <title>hello</title>
</html>*/
}.toString().match(/.*\r?\n([\s\S]*)\*\//)[1];

console.log(text);
//<html>
    //<title>hello</title>
//</html>

長所

  1. 事前の準備がいらない。

短所

  1. きたない。
  2. ブラウザの実装依存。
  3. 正規表現を書くのが面倒。
  4. minifyされると死ぬ。

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
4