I'm trying to make a script that will search for the words "ph" in the file name and if it will find it, it will store the ph+5 numbers after it.
At my work we give every job a number "ph32545" for example and I just want to check if the file name contains a ph number and store just that part in a variable.
This is what I have managed to do so far, the problem is that I don't know how to extract the ph number:
var doc = app.activeDocument;
var name = doc.name;
if (name.match("ph") == "ph") {alert("C1");}
the alert is just for testing to see if a ph has been found.
Just searching for ph will also give a match if a filename by coincidence contains these two letters. You should use a RegExp to make sure that ph is followed by five digits.
It could look like this: /ph\d{5}/.
The RegExp must have a / first and last. ph is obviously the text string, \d means "any digit" and {5} means "five times". (You could also just have written /ph\d\d\d\d\d/, but that's a little longer.)
If you don't add a g for "global" after the RegExp, the match method only searches for the first match and returns an array where the first item is the found match.
If there isn't a match, the method will return null.
So your example should be like this:
var doc = app.activeDocument;
var name = doc.name;
var match = name.match(/ph\d{5}/);
if (match) alert(match[0]);
Read more about String.prototype.match().