diff --git a/src/parser.js b/src/parser.js index adb525ad..629ff82c 100644 --- a/src/parser.js +++ b/src/parser.js @@ -76,9 +76,23 @@ const parser = (() => { var depth = 0; var pattern; var flags; + + var isClosingSlash = function (position) { + if (path.charAt(position) === '/' && depth === 0) { + var backslashCount = 0; + while (path.charAt(position - (backslashCount + 1)) === '\\') { + backslashCount++; + } + if (backslashCount % 2 === 0) { + return true; + } + } + return false; + }; + while (position < length) { var currentChar = path.charAt(position); - if (currentChar === '/' && path.charAt(position - 1) !== '\\' && depth === 0) { + if (isClosingSlash(position)) { // end of regex found pattern = path.substring(start, position); if (pattern === '') { diff --git a/test/implementation-tests.js b/test/implementation-tests.js index aedb0013..1abc214f 100644 --- a/test/implementation-tests.js +++ b/test/implementation-tests.js @@ -749,7 +749,38 @@ describe("Tests that are specific to a Javascript runtime", () => { }); }); + describe("empty regex: Escaped termination", function() { + it("should throw error", function() { + expect(function() { + var expr = jsonata("/\\/"); + expr.evaluate(); + }) + .to.throw() + .to.deep.contain({ position: 3, code: "S0302" }); + }); + }); + + describe("empty regex: Escaped termination", function() { + it("should throw error", function() { + expect(function() { + var expr = jsonata("/\\\\\\/"); + expr.evaluate(); + }) + .to.throw() + .to.deep.contain({ position: 5, code: "S0302" }); + }); + }); + describe("Functions - $match", function() { + describe('$match("test escape \\\\", /\\\\/)', function() { + it("should find \\", async function() { + var expr = jsonata('$match("test escape \\\\", /\\\\/)'); + var result = await expr.evaluate(); + var expected = { match: "\\", index: 12, groups: []}; + expect(result).to.deep.equal(expected); + }); + }); + describe('$match("ababbabbcc",/ab/)', function() { it("should return result object", async function() { var expr = jsonata('$match("ababbabbcc",/ab/)');