From fbbfa04a3400bd1673c70587c2062f2f761cabff Mon Sep 17 00:00:00 2001 From: Kim Biesbjerg Date: Tue, 20 Dec 2016 16:18:40 +0100 Subject: [PATCH] Use non-greedy regular expression --- src/parsers/pipe.parser.ts | 2 +- tests/parsers/pipe.parser.spec.ts | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/parsers/pipe.parser.ts b/src/parsers/pipe.parser.ts index d51294d..dc73fbb 100644 --- a/src/parsers/pipe.parser.ts +++ b/src/parsers/pipe.parser.ts @@ -15,7 +15,7 @@ export class PipeParser extends AbstractTemplateParser implements ParserInterfac protected _parseTemplate(template: string): TranslationCollection { let collection: TranslationCollection = new TranslationCollection(); - const regExp = new RegExp(/(['"`])([^\1]*)\1\s*\|\s*translate/, 'g'); + const regExp = new RegExp(/(['"`])([^\1]*?)\1\s*\|\s*translate/, 'g'); let matches: RegExpExecArray; while (matches = regExp.exec(template)) { diff --git a/tests/parsers/pipe.parser.spec.ts b/tests/parsers/pipe.parser.spec.ts index f177c90..2b7e4fa 100644 --- a/tests/parsers/pipe.parser.spec.ts +++ b/tests/parsers/pipe.parser.spec.ts @@ -36,4 +36,24 @@ describe('PipeParser', () => { expect(keys).to.deep.equal(['Hello World']); }); + it('should not use a greedy regular expression', () => { + const contents = ` + + + {{ 'Info' | translate }} + + + + + + + {{ 'Loading...' | translate }} + + + + `; + const keys = parser.extract(contents, templateFilename).keys(); + expect(keys).to.deep.equal(['Info', 'Loading...']); + }); + });