Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

Commit

Permalink
Fix escapeRegExp and add test
Browse files Browse the repository at this point in the history
  • Loading branch information
IllusionMH committed Jan 7, 2017
1 parent c8a03f4 commit 6fe20ce
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,5 +97,5 @@ export function stripComments(content: string): string {
* Escapes all special characters in RegExp pattern to avoid broken regular expressions and ensure proper matches
*/
export function escapeRegExp(re: string): string {
return re.replace(/[.+*?|^$\[]{}()\\]/g, "$&");
return re.replace(/[.+*?|^$[\]{}()\\]/g, "\\$&");
}
13 changes: 12 additions & 1 deletion test/utilsTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

import {arrayify, dedent, objectify} from "../src/utils";
import {arrayify, dedent, escapeRegExp, objectify} from "../src/utils";

describe("Utils", () => {
it("arrayify", () => {
Expand Down Expand Up @@ -46,4 +46,15 @@ describe("Utils", () => {
assert.equal(dedent` `, " ");
assert.equal(dedent``, "");
});

it("escapeRegExp", () => {
const plus = escapeRegExp("(a+|d)?b[ci]{2,}");
const plusRe = new RegExp(plus);

// contains substring that matches regular expression pattern
assert.equal(plusRe.test("regexpaaaabcicmatch"), false);

// properly matches exact string with special characters
assert.equal(plusRe.test("string(a+|d)?b[ci]{2,}match"), true);
});
});

0 comments on commit 6fe20ce

Please sign in to comment.