diff --git a/packages/babel-plugin-transform-regexp-constructors/__tests__/transform-regexp-constructors-test.js b/packages/babel-plugin-transform-regexp-constructors/__tests__/transform-regexp-constructors-test.js index a059a069d..13295fc2f 100644 --- a/packages/babel-plugin-transform-regexp-constructors/__tests__/transform-regexp-constructors-test.js +++ b/packages/babel-plugin-transform-regexp-constructors/__tests__/transform-regexp-constructors-test.js @@ -135,4 +135,14 @@ describe("transform-regexp-constructors-plugin", () => { var x = /\/x\//; ` ); + + thePlugin( + "should keep NUL", + String.raw` + var x = new RegExp('\0'); + `, + String.raw` + var x = /\0/; + ` + ); }); diff --git a/packages/babel-plugin-transform-regexp-constructors/src/index.js b/packages/babel-plugin-transform-regexp-constructors/src/index.js index 6aa64a4bb..ddd35d653 100644 --- a/packages/babel-plugin-transform-regexp-constructors/src/index.js +++ b/packages/babel-plugin-transform-regexp-constructors/src/index.js @@ -22,6 +22,9 @@ function createRegExpLiteral(args, prettify, t) { .replace(/\f/g, "\\f") .replace(/\r/g, "\\r"); } + + pattern = pattern.replace(/\0/g, "\\0"); + return t.regExpLiteral(pattern, flags); }