Skip to main content
Fightergator
Inspiring
Sep 16, 2022 5:34 PM UTC
Answered

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

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 is closed to new replies. Start a new post to keep the conversation going.
K.Daube
K.DaubeCorrect answer
Community Expert
Sep 22, 2022 7:39 AM UTC

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?

10 replies

frameexpert
Community Expert
Community Expert
Sep 19, 2022 11:43 AM UTC

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);
    }
}
Translate
www.frameexpert.com
Fightergator
Inspiring
Sep 20, 2022 2:51 PM UTC

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

Translate
K.Daube
Community Expert
Community Expert
Sep 17, 2022 8:05 AM UTC

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 

 

Translate
Fightergator
Inspiring
Sep 17, 2022 3:58 PM UTC

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?!

Translate