• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

If document name contain THIS, but NOT THIS

Explorer ,
Aug 01, 2016 Aug 01, 2016

Copy link to clipboard

Copied

I am in the process of writing the following script:

If the document name contains "front" and "square", do A.

If the document name contains "front" and NOT "square", do B.

If the document name contains "back" and "square, do C.

If the document name contains "back" and NOT "square", do D.

Here's the relevant code with the conditionals in the same order:

for (i = 0; i < documents.length; i++){

    var curDoc = app.activeDocument = app.documents;

    var workingName = curDoc.name;

    if(workingName.match(/^*(front)*$/) != null && workingName.match(/^*(square)*$/) != null) {...}

    if(workingName.match(/^*(front)*$/) != null && workingName.match(/^*(square)*$/) == null) {...}

    if(workingName.match(/^*(back)*$/) != null && workingName.match(/^*(square)*$/) != null) {...}

    if(workingName.match(/^*(back)*$/) != null && workingName.match(/^*(square)*$/) == null) {...}

}

However, these ARE not returning true and false as expected. As far as I can tell, taken individually, each regex pattern match works correctly.  I suspect the problem lies with the &&, ==null, and !=null parts, but am at a bit of a loss.

Specifically a file named "front.TIF" is being evaluated as TRUE for this conditional:

if(workingName.match(/^*(front)*$/) != null && workingName.match(/^*(square)*$/) != null)

if(workingName.match(/^*(front)*$/) != null && workingName.match(/^*(square)*$/) != null) {...}

This does not make sense, it does NOT contain the word "square" so the second half of this conditional, and therefore the entire thing, should be false. Instead it's treating the && (and) like an || (or).

Is (regexMatch != null && otherRegexMatch != null) a flawed approach?

TOPICS
Actions and scripting

Views

315

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Aug 01, 2016 Aug 01, 2016

I tried it with this, and the tiff was okay:

#target photoshop

for (i = 0; i < documents.length; i++){ 

    var curDoc = app.activeDocument = app.documents

    var workingName = curDoc.name; 

    if(workingName.match(/(front)/g) && workingName.match(/(square)/g)) {alert(workingName +'   ' +1 +'   '+ workingName.match(/(front)/g) + '   '+workingName.match(/(square)/g) )}  

    if(workingName.match(/(front)/g) && !workingName.match(/(square)/g)) {alert(workingName +'   ' +2 +'   '+ workingName.mat

...

Votes

Translate

Translate
Adobe
Community Expert ,
Aug 01, 2016 Aug 01, 2016

Copy link to clipboard

Copied

I tried it with this, and the tiff was okay:

#target photoshop

for (i = 0; i < documents.length; i++){ 

    var curDoc = app.activeDocument = app.documents

    var workingName = curDoc.name; 

    if(workingName.match(/(front)/g) && workingName.match(/(square)/g)) {alert(workingName +'   ' +1 +'   '+ workingName.match(/(front)/g) + '   '+workingName.match(/(square)/g) )}  

    if(workingName.match(/(front)/g) && !workingName.match(/(square)/g)) {alert(workingName +'   ' +2 +'   '+ workingName.match(/(front)/g) + '   '+workingName.match(/(square)/g) )}  

    if(workingName.match(/(back)/g) && workingName.match(/(square)/g)) {alert(workingName +'   ' +3)}  

    if(workingName.match(/(back)/g) && !workingName.match(/(square)/g)) {alert(workingName +'   ' +4)} 

    }

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 01, 2016 Aug 01, 2016

Copy link to clipboard

Copied

That first part of code is uneeded and global mode in RegExp too:

for(var i = 0; i < documents.length; i++){ 

    var workingName = documents.name

    if (workingName.match(/front/) && workingName.match(/square/)) alert('FS')

    if (workingName.match(/front/) && !workingName.match(/square/)) alert('F')

    if (workingName.match(/back/) && workingName.match(/square/)) alert('BS')

    if (workingName.match(/back/) && !workingName.match(/square/)) alert('B')

}

Btw. this is good place to test your RegEx before implementing in JavaScript code: RegExr: Learn, Build, & Test RegEx

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 01, 2016 Aug 01, 2016

Copy link to clipboard

Copied

LATEST

Yea,I thought about the global part and that it was unneeded. Still trying to wrap my head around regex.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines