Copy link to clipboard
Copied
Hello,
I am looking to determine the name of a file by using a regular expression. I have three files each one has a different file name that I want to change to depending on the input. The three inputs have either _S_, _F_,_B_ in the name and then I want the reg exp to change the name dependant on this. I can get the _F_ and _B_ to work but the file with _S_ in changes to _F_
Javascript is very new to me. This is what i have done.
var Packshot_S = new RegExp("_S_"); | ||
var Planogram_F = new RegExp("_F_"); | ||
var Planogram_B = new RegExp("_B_"); | ||
// create another variable that tests the text string against the pattern. If nothing is found, the test will return "-1". | ||
var TypeTester = FileRefName.search (Packshot_S); | ||
if (TypeTester == -1) { | ||
alert("Packshot_S: " + Packshot_S + TypeTester); | ||
//Is a Packshot | ||
var View = "S"; | ||
} | ||
var TypeTester = FileRefName.search (Planogram_F); | ||
if (TypeTester == -1) { | ||
alert("Planogram_F: " + Planogram_F + TypeTester); | ||
// Is a Planogram Front | ||
var View = "B"; | ||
} | ||
var TypeTester = FileRefName.search (Planogram_B); | ||
if (TypeTester == -1) { | ||
alert("Planogram_B: " + Planogram_B + TypeTester); | ||
// Is a Planogram Back | ||
var View = "F"; | ||
} |
Thanks
Chris
Copy link to clipboard
Copied
At first glance it looks to me like you have var View reversed for "B" and "F".
I would combine the RegExp into one expression and convert the multiple if statements into a switch statement.
var re = new RegExp('_[SFB]_');
var typeTester = fileRefName.match(re);
typeTester==null ? "":typeTester = typeTester[0];
switch( typeTester ){
case '_S_':
alert("Packshot_S: " + typeTester);
var View = "S";
break;
case '_F_':
alert("Planogram_F: " + typeTester);
var View = "F";
break;
case '_B_':
alert("Planogram_B: " + typeTester);
var View = "B";
break;
}
Also the standard JavaScript naming convention is to start variables with a lowercase letter.
Copy link to clipboard
Copied
Thank you Michael this has worked for me
Find more inspiration, events, and resources on the new Adobe Community
Explore Now