Re: Search method or RegExp?



On Aug 30, 1:24 am, "Evertjan." <exjxw.hannivo...@xxxxxxxxxxxx> wrote:

When testing with regex, use test(),
which returns a boolean value.

if (/(\S+@\S+\.\S+)/.test(sEmail)) {

better:

if (/^\S+@\S+\.\S{2,}$/.test(sEmail)) {

Why not use the last one?

return /^\S+@\S+\.\S\S+$/.test(sEmail);

Thanks all ... I looked over all the helpful suggestions, and have it
down to a simple one-liner. Evertjan, the last one you posted right
above didn't seem to like an address such as a@xxx

A boolean return value which the "test" method returns does seem more
intuitive than a check against -1. I'm guessing from the examples
everyone posted that using the "test" or even the "search" methods are
not any less supported by browsers than the RegExp object which I
don't recall anyone including in their examples. At any rate, I'm
going with the following validation test:

function checkEmail(sEmail) {
return (/(\S+@\S+\.\S+)/.test(sEmail));
}

Thanks all,
Ben

.



Relevant Pages

  • Re: Search method or RegExp?
    ... Ben Amada wrote on 30 aug 2007 in comp.lang.javascript: ... When testing with regex, use test, ... which returns a boolean value. ... don't recall anyone including in their examples. ...
    (comp.lang.javascript)
  • Re: Compare a Variable Against Multiple Values
    ... Consider using OrElse instead of Or, as Or will cause all the equations to ... I would use the RegEx as its IMHO the "simplest" & "cleanest" ... wordsAs String) As Boolean ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Compare a Variable Against Multiple Values
    ... you need to understand RegEx to be comfortable ... > the routine. ... > wordsAs String) As Boolean ...
    (microsoft.public.dotnet.languages.vb)
  • Re: regular expressions
    ... The hyphen is a special character in a set, you have to escape it: ... Function RegExpValidateas boolean ... Dim RegexObj as Regex = New Regex ...
    (microsoft.public.dotnet.languages.vb)
  • Re: regular expressions
    ... Function RegExpValidateas boolean ... Dim RegexObj as Regex = New Regex ... Dim IsMatch As Boolean = _ ... You do not need to instantiate 'Regex' because 'IsMatch' is a shared method of the 'Regex' class. ...
    (microsoft.public.dotnet.languages.vb)