エラーメッセージに正規表現を使いたい...
try {
throw new Error("Foo 'bar'");
} catch (err) {
console.log(getInQuotes(err));
}
... getInQuotes は文字列の関数です:
var getInQuotes = function(str) {
var re;
re = /'([^']+)'/g;
return str.match(re);
};
...しかし、エラーが発生しました:
Object Error: Foo 'bar' has no method 'match'
通常の文字列でも機能しますが:
console.log(getInQuotes("Hello 'world'"));
結果:
[ '\'world\'' ]
エラーオブジェクトを文字列化しようとしました...
console.log("stringify: " + JSON.stringify(err));
...しかし、それは空です:
stringify: {}