我试图在 Meteor Session 中为 MongoDB 查询将正则表达式保存为 RegExp 对象,但在 Session.get() 之后,RegExp 对象只是一个空对象。
js
if (Meteor.isClient) {
Meteor.startup(function () {
var obj = {};
obj['regexp'] = new RegExp("test");
console.log("setting regular expression: " + obj['regexp']);
Session.set("test", obj);
});
Template.test.events({
'click button': function () {
var regex = Session.get("test");
console.log("now it is: ");
console.log(regex['regexp']);
}
});
}
if (Meteor.isServer) {
}
html
<head>
<title>meteor-regexp-session-test</title>
</head>
<body>
{{> test}}
</body>
<template name="test">
<button>hit the button and look at the console</button>
</template>
知道为什么这不起作用吗?
提前致谢!
最佳答案
您需要保存正则表达式源:
var regex = new RegExp('test');
Session.set('regex', regex.source);
...
var restoredRegex = new RegExp(Session.get('regex'));
console.log(restoredRegex);
关于javascript - Meteor:将 RegExp 对象保存到 session ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25544805/