I need to find everything in a string that is not an e-mail address.
Here is my version of how to find an e-mail address.
^[a-zA-Z0-9_.-]+#[a-zA-Z0-9][a-zA-Z0-9-.]+\.([a-zA-Z]{2,6})$
I want to modify this regex to find the inverse--everything other than the e-mail address in any string.
###Example 1:
asdasd
###Example 2:
123#asd.com sda
Note: I want to get status == true in the following line:
var status = myString.match(pattern matches everything that is not an email address);
###I can only change the pattern, nothing else!
The official standard is known as RFC 2822. Regex pattern for email address is then:
(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")#(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])
More practical implementation of RFC 2822 (if we omit the syntax using double quotes and square brackets), which will still match 99.99% of all email addresses in actual use today, is:
[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*#(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?
To get list of non-matching "words" from myString use JavaScript code:
var status = myString.match(/(?:\s|^)(?![a-z0-9!#$%&'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+\/=?^_`{|}~-]+)*#(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)\S+\b(?=\s|$)/ig);
Check this demo.
You can call replace, with an empty string to remove the instances of the emails matching your pattern:
var noEmails = stringWithEmails.replace(/[a-zA-Z0-9_.-]+#[a-zA-Z0-9][a-zA-Z0-9-.]+\.([a-zA-Z]{2,6})/g, '');
Just note that I took out the leading ^ and trailing $. These were forcing the pattern to match the whole string (^ is beginning of line, and $ is end of line).
var result=myString.replace('[a-zA-Z0-9_.-]+#[a-zA-Z0-9][a-zA-Z0-9-.]+\.([a-zA-Z]{2,6})', '');
string.replace(/[/[a-zA-Z0-9_.-]+#[a-zA-Z0-9][a-zA-Z0-9-.]+.([a-zA-Z]{2,6})]*/gi, '');
its worked
Based on your edits, what you actually want is
var status = ! myString.match(email pattern)
That is, use your email pattern (or the giant thing posted by Ωmega) with the guards. That will match everything that is only an email address. That's exactly the opposite of what you want, so invert the boolean and you're done.
Related
I have this input string for a regular expression
ine",monitor_crashes:false,container_type:null,min_aspect_ratio:0.25,max_aspect_ratio:4,number_of_partitions:2,multi_partitioning_enabled:false,access_token:"EAAAAUaZA8jlABAOZC1TJwwFgfHyWt4V6b6B6cNxMXKkrjcpmzYS2vB7GWnIJFZCFQMPPEoZCInyJVigwcn8DtZA9xtYNATZBZBriOZBjAhdZCMfZCwohKOISSpC8aewclxA3U3X2PqPZBwZCdZBcKNA2Ydr2pQECR6ZBbuOaAZD",resumability_enabled:true,resumable_service_override:null,change_default_chunk_size:true,client_chunk_size:200000000,use_real_progress_percentage:false,use_progress_linearity:0,use_progress_transform_x:1,early_receive:false
I try to grab access token. But result return not single value.
I want single value:
"EAAAAUaZA8jlABAOZC1TJwwFgfHyWt4V6b6B6cNxMXKkrjcpmzYS2vB7GWnIJFZCFQMPPEoZCInyJVigwcn8DtZA9xtYNATZBZBriOZBjAhdZCMfZCwohKOISSpC8aewclxA3U3X2PqPZBwZCdZBcKNA2Ydr2pQECR6ZBbuOaAZD"
How to improvement my regex.
My test https://www.debuggex.com/r/xPqpBV3e9h2yoghE
My regex: (\w)+(?="|access_token$)
Your current regex, (\w)+(?="|access_token$), matches any length (>= 1) of word characters followed either by an " or by access_token$. I'm really not sure why you would want to have it followed by access_token$, because the access_token comes before the text you're looking for.
I don't know why a simple regex like: access_token:"(\w+)\" wouldn't work? (the first capturing group is the string) It looks for the string with the key access_token.
Then again, as #desoares said in the comments, it's probably best to parse this JSON with a JSON parser, using: JSON.parse(yourJsonObjectString).access_token.
This regular expression is what would work:
RegEx: access_token:"(\w+?)"
Click Here to see Screen Capture of Solution using your page "Debuggex.com"
Regex = ([A-Z]\w+)
Reference:
enter link description here
I have this RegEx which I use for CC and BCC email fields
reg = /(^\s*$|^[a-zA-Z0-9._%+-]+#[a-zA-Z0-9.-]+\.(?:[a-zA-Z]{2}|com|org|net|edu|gov|mil|biz|info|mobi|name|aero|asia|jobs|museum)$)/;
This allows for the email field to be empty, or have a valid email address, otherwise it will error.
I would like to extend the RegEx to allow mutiple emails also e.g. a#a.com, b#b.com, c#c.com
I have tried adding [,;] to allow comma or semicolon seperated values, but i can't seem to get it to work.
Any one know if i'm on the right lines with [,;] and where I should be placing it?
Update: I've updated the RegEx to, so it doesn't look for gTLDs:
reg =
/(^\s*$|^[a-zA-Z0-9._%+-]+#[a-zA-Z0-9.-]+.[A-Za-z]{2,4}[,;]?)+$/;
thanks
If Alex K.'s comment about ASP.NET validation doesn't help, then I have a band-aid for you. I wouldn't consider this a proper answer, as there really isn't a way to get exactly the functionality that you're looking for without giving us all pre and post email special characters that can occur. You could use something like this that uses non-capture groups to help find matches. It's not 100% accurate, but it should work for most cases. One problem with it is that you're apt to capture garbage/non-desired results if it runs into stray # symbols.
regex tested by RegexBuddy 4.2.0:
(?m)(?:^|\s|\n|\t|\r|,|;|
)[^\n]*?#[^\n]*?\.[^\n]*?(?:$|;|\s|,)
Test strings used:
9som$emaIL#cm3Gks.qa1vv; 9som$emaIL#cm3Gks.qa1vv, 9som$emaIL#cm3Gks.qa1vv; 9som$emaIL#cms.com ;
dd.dd.ddwe.wscef_sed#_e23&&*^.dvcw
I'm really new to regex in general. All I need is it to check and make sure (Something#something.something) works. I've tried this.
var checkEmail = /^\w+#\w+.[a-zA-Z]/;
Is something like this correct for what I'm looking for?
To refine what you have:
var checkEmail = /^\w+#\w+\.[a-zA-Z]+/;
What you posted it close (you should escape the . so it doesn't match any character and add a + after the [a-zA-Z] because top-level domains are at least 2 character I think), but for something like an email address that actually has a long and little known spec, I would just use someone else's regex.
Here's a site with more info:
http://www.regular-expressions.info/email.html
You should escape the dot, otherwise it is a meta character that matches anything. Try:
/^\w+#\w+.[a-zA-Z]/
I've inherited this javascript regex from another developer and now, even though nothing has changed, it doesn't seem to match the required text. Here is the regex:
/^.*(already (active|exists|registered)).*$/i
I need it to match any text that looks like
stuff stuff already exists more stuff etc
It looks perfectly fine to me, it only looks for those 2 words together and should in theory ignore the rest of the string. In my script I check the text like this
var cardUsedRE = /^.*(already (active|exists|registered)).*$/i;
if(cardUsedRE.test(responseText)){
mdiv.className = 'userError';
mdiv.innerHTML = 'The card # has already been registered';
document.getElementById('cardErrMsg').innerHTML = arrowGif;
}
I've stepped through this in FireBug and I've seen it fail to test this string:
> Error: <detail>Card number already registered for CLP.\n</detail>
Am I missing something? What is the likely issue with this?
Here's a simplified but functionally-equivalent regex that should handle newlines:
/(already\s+(active|exists|registered))/i
Not sure why you'd ever want to lead with ^.* or end with .*$ unless your goal is specifically to prevent newlines. Otherwise it's just superfluous.
EDIT: I replaced the space with \s+ so it will be more liberal with how it handles whitespace (e.g. one space, two spaces, a tab, etc. should all match).
tldr; Use the m modifier to make . match newlines. See the MDC regular expression documentation.
Failing (note the "\n" in the string literal):
var str = "Error: <detail>Card number already registered for CLP.\n</detail>"
str.match(/^.*(already (active|exists|registered)).*$/i)
Working (note m flag for "multi-line" behavior of .):
var str = "Error: <detail>Card number already registered for CLP.\n</detail>"
str.match(/^.*(already (active|exists|registered)).*$/mi)
I would use a simpler form, however: (Adjust for definition of "space".)
var str = "Error: <detail>Card number already registered for CLP.\n</detail>";
str.match(/(?:already\s+(?:active|exists|registered))/i)
Happy coding.
I'm using the following for email validation:
var filter = /^([\w]+)(.[\w]+)*#([\w]+)(.[\w]{2,3}){1,2}$/; // For Email Validation
if (filter.test(emailInputVal))) {console.log('good')}
For some reason the above does not work with emails that have a subdomain Any ideas why?
xxxx#xxx.xxx.com
Thanks
Because your regular expression is incorrect. Try this instead:
var filter = /^\w+(?:\.\w+)*#\w+(?:\.\w+)+$/;
This link may help you lots when validating email addresses:
http://www.regular-expressions.info/email.html
Official RFC 2822 standard
This non-trivial simplified regular expression conforming to RFC 2822 standard:
var filter = /[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*#(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+(?:[A-Z]{2}|com|org|net|edu|gov|mil|biz|info|mobi|name|aero|asia|jobs|museum)\b/;
That is one weird regex. It's certainly not doing what you're expecting it to do, for example because the dot isn't escaped when you do mean a literal dot.
Since it's impossible to really validate an email address with a regex anyway - why not go for something simpler?
/^[A-Z0-9._%+-]+#[A-Z0-9.-]+\.[A-Z]{2,6}$/i
This will still match some invalid addresses and will reject some valid addresses (as all readable regexes do), but in the end you have to send a confirmation mail to a user-submitted mail address and see if you get a reply if you truly want to validate it.
You can't reliably validate email addresses with regular expressions. What I'd do:
use a simple expression like /^[^#]+#([A-Za-z0-9-]+\.)*[A-Za-z0-9-]+$/ for client-side validation to catch typos
check the DNS record on the server-side
send a confirmation mail
Your last component is: any length word, then one or two instances of (dot, two-or-three-letter word). I would expect "xxxx#xxx.xxx.com" to work, but perhaps not more realistic examples like "xxxx#xxx.example.com" because your domain name is not a two-or-three-letter word.
Do yourself a favor: use simply /^[^# ]+#[^# ]+\.[^# ]+$/ More about this: http://nedbatchelder.com/blog/200908/humane_email_validation.html
I am not sure if the above code will work any format, because there is an extra ) in the if condition, removing it works for the sub domain too:
if (filter.test(emailInputVal)) {console.log('good')}