× {{alert.msg}} Never ask again
Get notified about new tutorials RECEIVE NEW TUTORIALS

Regex in meteor session.

Foysal
Dec 28, 2015
<p>Meteor doesn't allow you to store a Regex in the built in reactive session. So if you try to do the following -</p> <pre><code class="language-javascript">Session.set('searchQuery', new RegExp('^test', 'gi'))</code></pre> <p><strong>It's not gonna work!</strong></p> <p>An easy work around can be, storing the regex source and parsing it with regex again before using the source. The following code can do that <a href="http://stackoverflow.com/a/25545925/1079478">(found on a StackOverflow answer)</a></p> <pre><code class="language-javascript">// When storing in session var query = new RegExp('^test', 'gi'); Session.set('searchQuery', query.source); // when retrieving from session var query = Session.get('searchQuery'); var queryRegExp = new RegExp(query); // or simply store the string in the session and convert it to RegExp after retrieving it.</code></pre> <p> </p>
comments powered by Disqus