Skip to content

Instantly share code, notes, and snippets.

@ericchaves
Created April 30, 2012 17:21
Show Gist options
  • Save ericchaves/2560249 to your computer and use it in GitHub Desktop.
Save ericchaves/2560249 to your computer and use it in GitHub Desktop.
instanceof regexp evalutes to false inside a sandbox
var reg = /^[a-z]$/;
console.log('case 01: typeof /^[a-z]$/ returns %s', typeof reg);
console.log('case 02: /^[a-z]$/ instanceof RegExp returns %s', reg instanceof RegExp);
console.log('case 03: /^[a-z]$/.constructor.name === "RegExp" returns %s', reg.constructor.name === 'RegExp');
var util = require('util'),
vm = require('vm'),
sandbox = { u: reg };
vm.runInNewContext('out1 = /^[a-z]$/ instanceof RegExp; out2 = u instanceof RegExp; u_name = u.constructor.name', sandbox);
console.log(util.inspect(sandbox));
@mannharleen
Copy link

Is there a solution how to keep the regex object when passed to the vm?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment