Skip to content

Commit 58558f1

Browse files
authored
Merge pull request #16 from mcollina/regex-escape
Correctly escape regex string
2 parents 60fe6ad + 9c3db0b commit 58558f1

File tree

1 file changed

+1
-15
lines changed

1 file changed

+1
-15
lines changed

index.js

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ function build (schema, options) {
1818
${$asNumber.toString()}
1919
${$asNull.toString()}
2020
${$asBoolean.toString()}
21-
${$asRegExp.toString()}
2221
`
2322
var main
2423

@@ -79,27 +78,14 @@ function $asString (str) {
7978
if (str instanceof Date) {
8079
return '"' + str.toISOString() + '"'
8180
} else if (str instanceof RegExp) {
82-
return $asRegExp(str)
81+
str = str.source
8382
} else if (typeof str !== 'string') {
8483
str = str.toString()
8584
}
8685

8786
return JSON.stringify(str)
8887
}
8988

90-
function $asRegExp (reg) {
91-
reg = reg.source
92-
93-
for (var i = 0, len = reg.length; i < len; i++) {
94-
if (reg[i] === '\\' || reg[i] === '"') {
95-
reg = reg.substring(0, i) + '\\' + reg.substring(i++)
96-
len += 2
97-
}
98-
}
99-
100-
return '"' + reg + '"'
101-
}
102-
10389
function addPatternProperties (schema, externalSchema) {
10490
var pp = schema.patternProperties
10591
let code = `

0 commit comments

Comments
 (0)