Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is meaning of [_|\_|\.]? in Javascript regexps?

I have a js code:

/^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/

But what's meaning of [_|\_|\.]?(js regexp)

like image 329
JackSun Avatar asked Nov 29 '22 07:11

JackSun


2 Answers

If we use a resource like Regexper, we can visualise this regular expression:

Example

From this we can conclude that [_|\_|\.] requires one of either "_", "|" or ".". We can also see that the double declaration of "_" and "|" is unnecessary. As HamZa commented, this segment can be shortened to [_|.] to achieve the same result.

In fact, we can even use resources like Regexper to visualise the entire expression.

like image 141
James Donnelly Avatar answered Dec 05 '22 18:12

James Donnelly


REGEX101 is a very good tool for understanding regular expression

Char class [_|\_|\.] 0 to 1 times [greedy] matches:

[_|\_|\. One of the following characters _|_|.
 [_|\_|\.] requires one of either "_", "|" or "."

See This Link of RegEx101 here Your Expression explanation

like image 27
Vinay Pratap Singh Bhadauria Avatar answered Dec 05 '22 16:12

Vinay Pratap Singh Bhadauria