How can I check if my string contains a period in JavaScript?
Use indexOf()
var str="myfile.doc";
var str2="mydirectory";
if(str.indexOf('.') !== -1)
{
// would be true. Period found in file name
console.log("Found . in str")
}
if(str2.indexOf('.') !== -1)
{
// would be false. No period found in directory name. This won't run.
console.log("Found . in str2")
}
Just test the return value of the indexOf
method: someString.indexOf('.') != -1
. No need for a regex.