7

I use tslint and when I write long regexp in typescript

var re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;

I got error - exceeds maximum line length of 140.

Does anybody know how to write it in 2 lines. I can do that with a hack. But I'm not satisfied with this solution.

    var r1 = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))/;
    var r2 = /@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
    var re = new RegExp(r1.source + r2.source);
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
radzserg
  • 1,258
  • 1
  • 13
  • 22
  • 1
    How is that related to Angular2? – Günter Zöchbauer Feb 24 '16 at 10:59
  • Also check [Casimir's comment](http://stackoverflow.com/questions/35561010/angular-internet-routable-email-address-validation#comment58811760_35561678) regarding a similar email validation regex. – Wiktor Stribiżew Feb 24 '16 at 12:01
  • I use angular2-seed boilerplate app. And tslint settings are pre-set there. – radzserg Feb 24 '16 at 12:36
  • Yes this is duplicate [How to split a long regular expression into multiple lines in JavaScript?](http://stackoverflow.com/questions/12317049/how-to-split-a-long-regular-expression-into-multiple-lines-in-javascript) Still I hoped that more elegant solution exists – radzserg Feb 24 '16 at 12:40

1 Answers1

3

Why not use strings?

var r1 = "^(([^<>()\[\]\\.,;:\s@\"]+(\.[^<>()\[\]\\.,;:\s@\"]+)*)|(\".+\"))";
var r2 = "@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$";
var re = new RegExp(r1 + r2);

RegExp(string) is easier for modification and/or dynamically generated regex

buræquete
  • 14,226
  • 4
  • 44
  • 89