wawanopoulos
wawanopoulos

Reputation: 9804

Regex Javascript in order to know if a pattern is containing in a String

I have a JavaScript String, for example :

var test1 = "[email protected]" ;  // Function must return true in this case

var test2 = "[email protected] " ;    // Function must return false in this case

I would like to build a function which return true if the String contains @ character, following by any character "bbbbbb" following by ".temp" extension.

How can I do this ?

Upvotes: 0

Views: 77

Answers (2)

PingOfDeath
PingOfDeath

Reputation: 137

Regular expression solution:

function validate(argument) { 
var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
    if((argument.lastIndexOf('.temp', argument.length-5)>=argument.length-5) && re.test(argument)) {
        return true;
    } else {
        return false;
    }
}

JsFiddle test: http://jsfiddle.net/2EXxx/

Upvotes: 0

anubhava
anubhava

Reputation: 786136

You can use regex#test

var found = /@.*?\.temp\b/.test(str);

If you want to make it @ character, following by "bbbbbb" following by ".temp" extension then use:

var found =  /@bbbbbb\.temp\b/.test(str);

Upvotes: 5

Related Questions