Skip to content

Commit 4f949ab

Browse files
committed
fix: string should not handled as RegExp
1 parent b0b77d3 commit 4f949ab

File tree

10 files changed

+35
-22
lines changed

10 files changed

+35
-22
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ RegExp-like String:
8484

8585
```json
8686
[
87-
"a \\w+"
87+
"/a \\w+/"
8888
]
8989
```
9090

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
"tabWidth": 4
4545
},
4646
"devDependencies": {
47+
"@types/escape-string-regexp": "^0.0.32",
4748
"@types/lodash.sortby": "^4.7.3",
4849
"@types/lodash.uniq": "^4.5.3",
4950
"@types/lodash.uniqwith": "^4.5.3",
@@ -68,6 +69,7 @@
6869
]
6970
},
7071
"dependencies": {
72+
"escape-string-regexp": "^1.0.5",
7173
"execall": "^1.0.0",
7274
"lodash.sortby": "^4.7.0",
7375
"lodash.uniq": "^4.5.0",

src/regexp-parse.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const REGEXP_LITERAL_PATTERN = /^\/(.*)\/([guimy]*)$/;
2+
export const parseRegExpString = (str: string): { source: string; flagString: string } | null => {
3+
const result = str.match(REGEXP_LITERAL_PATTERN);
4+
if (!result) {
5+
return null;
6+
}
7+
return {
8+
source: result[1],
9+
flagString: result[2]
10+
};
11+
};
12+
export const isRegExpString = (str: string): boolean => {
13+
return REGEXP_LITERAL_PATTERN.test(str);
14+
};

src/regexp-string-matcher.ts

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,12 @@
11
import uniq = require("lodash.uniq");
22
import uniqWith = require("lodash.uniqwith");
33
import sortBy = require("lodash.sortby");
4+
import escapeStringRegexp = require("escape-string-regexp");
5+
import { isRegExpString, parseRegExpString } from "./regexp-parse";
46

57
const execall = require("execall");
68
const toRegex = require("to-regex");
7-
const REGEXP_LITERAL_PATTERN = /^\/(.*)\/([guimy]*)$/;
8-
const parseRegExpString = (str: string): { source: string; flagString: string } | null => {
9-
const result = str.match(REGEXP_LITERAL_PATTERN);
10-
if (!result) {
11-
return null;
12-
}
13-
return {
14-
source: result[1],
15-
flagString: result[2]
16-
};
17-
};
18-
const isRegExpString = (str: string): boolean => {
19-
return REGEXP_LITERAL_PATTERN.test(str);
20-
};
9+
2110
const DEFAULT_FLAGS = "g";
2211

2312
const defaultFlags = (flagsString: string) => {
@@ -35,7 +24,7 @@ export interface matchPatternResult {
3524

3625
const createRegExp = (patternString: string): RegExp => {
3726
if (patternString.length === 0) {
38-
throw new Error("Emtpy string can not includes");
27+
throw new Error("Empty string can not includes");
3928
}
4029
if (isRegExpString(patternString)) {
4130
const regExpStructure = parseRegExpString(patternString);
@@ -47,10 +36,7 @@ const createRegExp = (patternString: string): RegExp => {
4736
}
4837
throw new Error(`"${patternString}" can not parse as RegExp.`);
4938
} else {
50-
return toRegex(patternString, {
51-
flags: DEFAULT_FLAGS,
52-
contains: true
53-
});
39+
return new RegExp(escapeStringRegexp(patternString), DEFAULT_FLAGS);
5440
}
5541
};
5642

test/example-test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ GitHub launched in 2018-04-10.`;
2222
]);
2323
});
2424

25-
it("example", () => {
25+
it("RegExp-like string", () => {
2626
const inputText = `This is a pen.`;
2727
// RegExp like strings
28-
const inputPatterns = ["a \\w+"];
28+
const inputPatterns = ["/a \\w+/"];
2929

3030
const results = matchPatterns(inputText, inputPatterns);
3131
assert.deepStrictEqual(results, [{ match: "a pen", startIndex: 8, endIndex: 13 }]);
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[
2+
"\\d+"
3+
]
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
GitHub is a web-based hosting service for version control using git.
2+
It is mostly used for computer code.
3+
GitHub launched in 2018-04-10.

test/snapshots/not-treat-regexp-string/output-for-human.md

Whitespace-only changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[]

yarn.lock

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@
2222
dependencies:
2323
any-observable "^0.3.0"
2424

25+
"@types/escape-string-regexp@^0.0.32":
26+
version "0.0.32"
27+
resolved "https://registry.yarnpkg.com/@types/escape-string-regexp/-/escape-string-regexp-0.0.32.tgz#296005808f51d27fb2a2de8d1ca080a91ee3375c"
28+
2529
"@types/lodash.sortby@^4.7.3":
2630
version "4.7.3"
2731
resolved "https://registry.yarnpkg.com/@types/lodash.sortby/-/lodash.sortby-4.7.3.tgz#d1f7a7fd955246e685a7541ac184dc058c553eb5"

0 commit comments

Comments
 (0)