JavaScript文字列から文字列リテラルを確実に取得する関数を記述したいと思います。f
と呼ぶことができます。例えばJavaScript内の文字列のリテラルを含む文字列
:私はあまりにも多くの単一引用符の事を気にしない、任意の文字列str
str = eval(f(str))
ため
f('hello world') //-> 'hello world' (or "hello world")
f('hello "world"') //-> 'hello "world" (or "hello \"world\"")
f("hello 'world'") //-> "hello 'world'"
f("hello \"'world'\"") //-> "hello \\\"'world'\\\""
f("hello \n world") //-> "hello \\n world"
そしてそう。
var f = function(str) {
return '"' + str.replace(/"/g, '\"') + '"';
}
をそれは明らかに、すべてをカバーしていない:
私は現在だけでやっています。
これはドキュメントシステム用です。使用
ああ、それは '\ n'では機能しません。私はあなたの問題を今理解しています。 –