Skip to main content
Fightergator
Inspiring
September 16, 2022
Answered

Clone Case of Replacement String in RegExp Find/Replace in ExtendScript

  • September 16, 2022
  • 2 replies
  • 831 views

Looking for thoughts on how to clone case of the first letter in my find/replace script.  For example, I search for \b[Aa]ll inclusive.  It finds "All inclusive."  When found, the replacement text is "inclusive," but I'm not smart enough with regexp or scripting to figure out a way make the first letter of the replacement text upper case.  

 

Was thinking along the lines of getting the first letter of the found text string, determine it's ascii no., then replacing the first letter of the replacement string with it's asci(no) - 32 if uppercase.  Sounds easy on paper, but not so much fun trying to script it.  I'm guessing their's an easier way.  Any thoughts appreciated. 

This topic has been closed for replies.
Correct answer K.Daube

Thank You Rick, for explaining the algrothm behind "Clone Case".

When looking at the second body row in the table below It seems to me that this algorithm is applied to each word in the replacement text.

Find Change Found before change Found and changed
all lower case AnoTher TexTual enTity all lower case another textual entity
mixed case AnoTher TexTual enTity MiXed Case AnoTher Textual enTity
all upper case AnoTher TexTual enTity ALL UPPER CASE ANOTHER TEXTUAL ENTITY

→ Is this also Your perception?

2 replies

frameexpert
Community Expert
Community Expert
September 19, 2022

Here are some snippets from my FindChangeBatch script, which supports Clone Case. I have added some comments so you can follow what I am doing:

var find, change;

find = "Star";
change = "bucks";

// If Clone Case is selected, make sure the first character in the change string 
// matches the case of the find string.
change = getCloneCaseString (find, change);

alert (change);

function getCloneCaseString (find, change) {
    
    var firstLetter;
    
    // Get the first letter of the find string and check its case.
    firstLetter = find[0];
    if (firstLetter === firstLetter.toUpperCase ()) {
        // If the first letter of the find string is uppercase, then
        // convert the first letter of the change string to uppercase.
        return change.charAt (0).toUpperCase () + change.slice (1);
    }
    else {
        // Otherwise, convert the first letter of the change string
        // to lowercase.
        return change.charAt (0).toLowerCase () + change.slice (1);
    }
}
Fightergator
Inspiring
September 20, 2022

Thanks Rick...that was exactly what I was looking for.  Much easier than the solution I was considering. 

K.Daube
Community Expert
Community Expert
September 17, 2022

FM/ES does not support the replacement Case Conversion, even not in the Perl flavour: neither \Ux \Lx or \Ix  nor $Ux $Lx or $Ix. The RegEx mechanism in my favourite editor EditPad Pro supports these 

 

Fightergator
Inspiring
September 17, 2022

Thanks.  That's what I suspected.  We've been using Word's find/replace until now (which doesn't support this either).  The problem is that the editors have to locate each Word find/replaces occurance in Frame and make the change manually.  I'm trying to cut that step out of the process.  Being able to clone the case isn't critical, but would further expedite the process.  Was hoping someone might have developed a function to do this.  If not, I'll try and stretch my limited scripting skills...another opportunity to learn, right?!