/    Sign up×
Community /Pin to ProfileBookmark

Javascript regex help needed

Hi guys

I need a regex to provide these outputs

[email][email protected][/email] —-> (false, @ preceded by an alpha numeric)
@john —-> (true, @ at start of line)
@john —->(true, @ preceded with any non alpha numeric)

At the moment I have ‘/@(w+)/ig;’
But its matching on the ‘[email protected]‘ when I don’t want it to because the ‘@’ is preceded by an alpha numeric.

to post a comment
JavaScript

8 Comments(s)

Copy linkTweet thisAlerts:
@SagebrushfireMar 31.2015 — Using the following negative lookup gets you almost where you want:

[B]/(?![A-z]).@(w+)/[/B]

If you create a new Regexp Object you can test it with [B].test("string")[/B]:

<i>
</i>var myExpression= new RegExp(/(?![A-z]).@(w+)/);

myExpression.test("@john"); // etc.



It returns False for [B][email protected][/B] and it returns true for [B]$@john[/B] or "[B] @john[/B]" (Space before @ Symbol).

Unfortunately for some reason it returns false for [B]@John[/B] (First character is @ symbol).

As a work-around you can check for those cases separately because the function [B]YourString.indexOf("@")[/B] will return 0 if "@" is the first character. I'm sure there's just something small missing from my RegEx but that at least is a workable temporary solution.


[B]EDIT:[/B]

I found an expression that [B]should[/B] work but for some reason it doesnt: [B]/^@/gi | (?![A-z]).@(w+)/[/B]

That should give you either the first character as an @ symbol or the other expression that returns false if its preceded by A-z. Unfortunately it doesn't because despite reading up on the pipe operator for almost an hour, I apparently don't understand it (Really RegExp is the most powerful and most hated thing in Programming; It's a huge pain.).

So until we figure out what's wrong with the regular expression that should be able to do it all in one go, here's how I'd write the function to use indexOf and the regex (No use in doing two regexp operations if you don't need to):

<i>
</i>
function isNotAnEmail(inputString)
{
if(inputString.indexOf("@") === 0)
{
return true;
}
else{

<i> </i> var reg = new RegExp(/(?![A-z]).@(w+)/);

<i> </i> return reg.test(inputString);
<i> </i>}
}

isNotAnEmail("[email protected]"); // Returns False

isNotAnEmail("@john"); // Returns True

isNotAnEmail(" @john"); // Returns True


Copy linkTweet thisAlerts:
@Kevin2Apr 01.2015 — Question: Why are we still validating email addresses with javascript in 2015?

<input [COLOR="#FF0000"]type="email"[/COLOR]> works in all relatively modern browsers even when javascript is turned off or blocked.
Copy linkTweet thisAlerts:
@SagebrushfireApr 01.2015 — Question: Why are we still validating email addresses with javascript in 2015?

<input [COLOR="#FF0000"]type="email"[/COLOR]> works in all relatively modern browsers even when javascript is turned off or blocked.[/QUOTE]


I didn't think this was for e-mail validation. If so then there are a ton of better ways to validate emails than to try and write your own code for it.

However [B]type="email"[/B] isn't supported by older versions of Internet Explorer so it's not a great idea to rely solely on the input type if you might have a user base using old browsers (As someone who has worked with federal and local governments quite a bit, this is a much larger population of people than you'd think).

Regardless of the purpose; the question was about Regex, not email validation.
Copy linkTweet thisAlerts:
@Kevin2Apr 01.2015 — Read the first word of the thread title ([B][I]Javascript[/I][/B] regex help needed) again.

Javascript, as you know, is a client-side language. How are you going to get, client-side, an e-mail address? Through a form input.

You kind of make my point though with your example of government Jurassic-era computers running Cretaceous-era versions of Internet Explorer. I get enough of those hits from a nearby Air Force Base on one website I manage. To say I cringe when I see those DoD XP/IE8- hits only begins to cover it. Why they are let outside the firewall is beyond my comprehension. The point I was originally trying to make with my rhetorical question is that one should be validating on the server, not on the browser. Any "validation" beyond the code I posted should not be a client-side task.

So while this thread was partially about RegEx, my question addressed a broader, but germane, subject.
Copy linkTweet thisAlerts:
@SagebrushfireApr 01.2015 — Read the first word of the thread title ([B][I]Javascript[/I][/B] regex help needed) again.

Javascript, as you know, is a client-side language. How are you going to get, client-side, an e-mail address? Through a form input.

You kind of make my point though with your example of government Jurassic-era computers running Cretaceous-era versions of Internet Explorer. I get enough of those hits from a nearby Air Force Base on one website I manage. To say I cringe when I see those DoD XP/IE8- hits only begins to cover it. Why they are let outside the firewall is beyond my comprehension. The point I was originally trying to make with my rhetorical question is that one should be validating on the server, not on the browser. Any "validation" beyond the code I posted should not be a client-side task.

So while this thread was partially about RegEx, my question addressed a broader, but germane, subject.[/QUOTE]


Read the title and the entire original post again. The word [B]E-mail[/B] is not used a single time in [B]eakin[/B]'s post. This is a question about JavaScript Regular Expressions; there is honestly not even a clear indication that this is related to form processing or email validation.

The expression could be for processing Twitter handles; it could be for isolating E-mail addresses from blocks of text to turn them into "mailto" links. Heck the original example requires an actual e-mail address to return [B]false[/B] ... how many e-mail validation scripts have you ever seen seek out non-email formats and only casually try to match legitimate ones? I daresay the output provided would be suboptimal for E-mail validation since you only care about valid addresses, not invalid ones.

Cynical rhetoric about a potentially relevant (but never explicitly mentioned) subject is hardly germane. Information about the "email" input type could be useful and its certainly appreciated but maybe leave the wild assumptions at home.

And to answer your second question: Just the other day I created a front-end for an event registration data sheet that contained e-mail addresses as part of a table of registered users. This was JSON data delivered to my script from a web service and it contained quite a number of e-mail addresses. I had to use a regular expression to check for email addresses from a particular domain and to correct e-mail addresses that referenced a subdomain of that domain before presenting them to the user (e.g. If the user accidentally put [email][email protected][/email] I stripped out the "exchange." for display purposes). No form inputs were involved.
Copy linkTweet thisAlerts:
@Vikas_PatelApr 01.2015 — The information given all above are very helpful regex for the javascript.
Copy linkTweet thisAlerts:
@eakinauthorApr 01.2015 — My question has NOTHING to do with email validation on forms.

I am writing JavaScript to detect the @ symbol followed by text so that I can display a list of names. I.e. to implement a tag functionality similar to Facebook.

I DONT want the list of names to be displayed if someone is typing an email address and the @ is detected. I thought this was clear in my original post but obviously it wasn't.
Copy linkTweet thisAlerts:
@Kevin2Apr 01.2015 — mea culpa and my apologies.
×

Success!

Help @eakin spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...