How can i do to search if a Javascript String contains the following pattern :
"@aRandomString.temp"
I would like to know if the String contains @ character and then any String and then ".temp" string.
Thanks
How can i do to search if a Javascript String contains the following pattern :
"@aRandomString.temp"
I would like to know if the String contains @ character and then any String and then ".temp" string.
Thanks
This one liner should do the job using regex#test(Strng):
var s = 'foo bar @aRandomString.temp baz';
found = /@.*?\.temp/i.test(s); // true
Use indexOf to find a string within a string.
var string = "@aRandomString.temp";
var apos = string.indexOf("@");
var dtemp = string.indexOf(".temp", apos); // apos as offset, invalid: ".temp @"
if (apos !== -1 && dtemp !== -1) {
var aRandomString = string.substr(apos + 1, dtemp - apos);
console.log(aRandomString); // "aRandomString"
}
You can use the match function. match expects the regular expression.
function myFunction()
{
var str="@someting.temp";
var n=str.test(/@[a-zA-Z]+\.temp/g);
}
Here is a demo: http://jsbin.com/IBACAB/1