Skip to content

Commit c0d5579

Browse files
committed
fix(RegExp): properly escape single quotes
1 parent 017024a commit c0d5579

File tree

2 files changed

+3
-2
lines changed

2 files changed

+3
-2
lines changed

lib/literal.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ class LiteralTranspiler extends base.TranspilerBase {
9999
var slashIdx = regExp.lastIndexOf('/');
100100
var flags = regExp.substring(slashIdx + 1);
101101
regExp = regExp.substring(1, slashIdx); // cut off /.../ chars.
102-
regExp = regExp.replace('\'', '\' + "\'" + r\''); // handle nested quotes by concatenation.
102+
regExp = regExp.replace(/'/g, '\' + "\'" + r\''); // handle nested quotes by concatenation.
103103
this.emitNoSpace(regExp);
104104
this.emitNoSpace('\'');
105105
if (flags.indexOf('g') === -1) {
@@ -133,4 +133,4 @@ class LiteralTranspiler extends base.TranspilerBase {
133133
}
134134
}
135135

136-
export = LiteralTranspiler;
136+
export = LiteralTranspiler;

test/literal_test.ts

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ describe('literals', () => {
4343
it('translates regexp literals', () => {
4444
expectTranslate('/wo\\/t?/g').to.equal(' new RegExp ( r\'wo\\/t?\' ) ;');
4545
expectTranslate('/\'/g').to.equal(' new RegExp ( r\'\' + "\'" + r\'\' ) ;');
46+
expectTranslate('/\'o\'/g').to.equal(' new RegExp ( r\'\' + "\'" + r\'o\' + "\'" + r\'\' ) ;');
4647
expectTranslate('/abc/gmi')
4748
.to.equal(' new RegExp ( r\'abc\' , multiline: true , caseSensitive: false ) ;');
4849
expectErroneousCode('/abc/').to.throw(/Regular Expressions must use the \/\/g flag/);

0 commit comments

Comments
 (0)