Copy link to clipboard
Copied
Hello guys! How do I check if there is a specific name at the beginning or end of the current document name?
It would be something like this:
"test myDocumente.jpg" or "myDocumente test.jpg"
Edited
I missed that both at the beginnang and at the end should qualify.
I expect one RegExp would be possible but two seems easier.
var theRegExp = new RegExp(/^test/i);
var theRegExp2 = new RegExp(/test$/i);
var docName = activeDocument.name;
try {var basename = docName.match(/(.*)\.[^\.]+$/)[1]}
catch (e) {basename = docName};
if (basename.match(theRegExp) != null || basename.match(theRegExp2) != null) {alert ("yes")}
else {alert ("no")};
Some other common minor variations include:
var docName = activeDocument.name.replace(/\.[^\.]+$/, '');
if (/ test$|^test /i.test(docName) === true) {
alert("True");
} else {
alert("False");
}
var docName = activeDocument.name.replace(/\.[^\.]+$/, '');
if (docName.match(/ test$|^test /i)) {
alert("True");
} else {
alert("False");
}
var docName = activeDocument.name.split('.')[0];
if (docName.match(/ test$|^test /i)) {
alert("True");
} els
...
Copy link to clipboard
Copied
Hi, what do you mean it should save with that name?
Copy link to clipboard
Copied
Edited
I missed that both at the beginnang and at the end should qualify.
I expect one RegExp would be possible but two seems easier.
var theRegExp = new RegExp(/^test/i);
var theRegExp2 = new RegExp(/test$/i);
var docName = activeDocument.name;
try {var basename = docName.match(/(.*)\.[^\.]+$/)[1]}
catch (e) {basename = docName};
if (basename.match(theRegExp) != null || basename.match(theRegExp2) != null) {alert ("yes")}
else {alert ("no")};
Copy link to clipboard
Copied
Perfect, that's exactly what I needed. Thanks @c.pfaffenbichler
Copy link to clipboard
Copied
@c.pfaffenbichler wrote:I expect one RegExp would be possible but two seems easier.
Here it is in a single regular expression, I included the leading/trailing white space to tighten up the search:
var theRegExp = new RegExp(/ test$|^test /i);
var docName = activeDocument.name;
try {var basename = docName.match(/(.*)\.[^\.]+$/)[1]}
catch (e) {basename = docName}
if (basename.match(theRegExp) !== null) {alert ("yes")}
else {alert ("no")}
Copy link to clipboard
Copied
Some other common minor variations include:
var docName = activeDocument.name.replace(/\.[^\.]+$/, '');
if (/ test$|^test /i.test(docName) === true) {
alert("True");
} else {
alert("False");
}
var docName = activeDocument.name.replace(/\.[^\.]+$/, '');
if (docName.match(/ test$|^test /i)) {
alert("True");
} else {
alert("False");
}
var docName = activeDocument.name.split('.')[0];
if (docName.match(/ test$|^test /i)) {
alert("True");
} else {
alert("False");
}
Copy link to clipboard
Copied
Very good @Stephen_A_Marsh ! Thanks for sharing