简体   繁体   中英

Javascript Regular Expression - Limit number of valid characters

I'm learning regular expressions in javascript and I think there is something I'm missing.

I'm using an example where I'm trying to extract valid email addresses from a string. I'm getting the valid emails but I'm also getting invalid ones. Here's the code:

var teststring = "This is my test string with a valid email: this@that.com,
             and an invalid email: this@broken.1. Pull only the valid email.";

teststring.match(/[A-Za-z0-9_+.-]+@[A-Za-z0-9]+.[A-Za-z]{2,3}/g)

When I run the match method, I get both the valid email "this@that.com" and the invalid email "this@broken.1" returned.

I thought the {2,3} at the end of the last square brackets was supposed to specify that the particular character search within the brackets should only be valid if they contain 2 to 3 instances of the criteria, so why does the broken email with just the "1" after the last dot get returned?

I should also add that I totally understand that this is not a be all end all email validation expression. This is purely a trying-to-understand-regular-expressions question for me. I was searching around for a clear answer but couldn't find exactly what I was looking for.

Thanks

. will match any character. To match an actual . you need \\.

teststring.match(/[A-Za-z0-9_+.-]+@[A-Za-z0-9]+\.[A-Za-z]{2,3}/g)

You need to escape that last . . Otherwise, it means "match any character", so the expression is catching these chunks:

this
@
brok
en

try:

teststring.match(/[A-Za-z0-9_+.-]+@[A-Za-z0-9]+\.[A-Za-z]{2,3}/g)

Since you're looking for capital or lowercase letters, you can simplify your search by making it case insensitive. the /g at the end of the regular expression makes the match global (ie return all such matches); you could instead use /i if you wanted only one match but you didn't care about case. For example,

"CaSe InSeNsItIvE iS cOoL; I lOvE cAsEs".match(/case/i)

returns the one-element array ["CaSe"]. To get all case-insensitive matches, just use /gi:

"CaSe InSeNsItIvE iS cOoL; I lOvE cAsEs".match(/case/gi)

returns ["CaSe", "cAsE"]

Your query can be shortened to

teststring.match(/[A-Z0-9_+.-]+@[A-Z0-9]+\.[A-Z]{2,3}/gi)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM