Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript: How to write long regexp in 2 lines [duplicate]

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);
like image 939
radzserg Avatar asked Feb 24 '16 10:02

radzserg


People also ask

What is multiline flag in regex?

The m flag indicates that a multiline input string should be treated as multiple lines. For example, if m is used, ^ and $ change from matching at only the start or end of the entire string to the start or end of any line within the string. The set accessor of multiline is undefined .

What is multiline regex?

Multiline option, or the m inline option, enables the regular expression engine to handle an input string that consists of multiple lines. It changes the interpretation of the ^ and $ language elements so that they match the beginning and end of a line, instead of the beginning and end of the input string.

How do you search for multiple words in a regular expression?

However, to recognize multiple words in any order using regex, I'd suggest the use of quantifier in regex: (\b(james|jack)\b. *){2,} . Unlike lookaround or mode modifier, this works in most regex flavours.

How do you clone a regular expression in JavaScript?

We can clone a given regular expression using the constructor RegExp(). Here regExp is the expression to be cloned and flags determine the flags of the clone. There are mainly three types of flags that are used. g:global flag with this flag the search looks for global match.


1 Answers

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

like image 177
buræquete Avatar answered Oct 13 '22 11:10

buræquete