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

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

Contributor ,
Sep 16, 2022 Sep 16, 2022

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. 

TOPICS
Scripting
539
Translate
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 2 Correct answers

Community Expert , Sep 19, 2022 Sep 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 str
...
Translate
Community Expert , Sep 22, 2022 Sep 22, 2022

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

...
Translate
Community Expert ,
Sep 17, 2022 Sep 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 

 

Translate
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
Contributor ,
Sep 17, 2022 Sep 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?!

Translate
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 ,
Sep 19, 2022 Sep 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);
    }
}
Translate
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
Contributor ,
Sep 20, 2022 Sep 20, 2022

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

Translate
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 ,
Sep 22, 2022 Sep 22, 2022

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?

Translate
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 ,
Sep 22, 2022 Sep 22, 2022

My algorithm just applies to the first letter in the find/change strings.

Translate
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
Contributor ,
Sep 22, 2022 Sep 22, 2022

Thanks to both of you for the code snippet and the table explaining the Clone Case algorithm.  The solution I was considering was to clone the first letter as well, which will cover some 99% of the found strings.  The script I've developed, with help from both of you, uses FM Track Edits.  I want our editors to accept or reject each edit. In the rare instance when cloning the first letter doesn't do the trick, the editor can fix it. 

Translate
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 ,
Sep 22, 2022 Sep 22, 2022

Yes, Rick, it's clear from the code that Your algorithm handles only the first character in the found/replaced item. However I wanted to find out the workings of the FM algorithm. Hence I modified my table to get a clearer picture:

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

Here it seems that all word in the found string are treated similar: In the first and last case IMHO not only  their first character is checked and handled. If the replacement is longer than the search, additional words are treated similar to the last word in the search.

My assumption is, that Your algorithm is sufficient in most use cases - but the FM algorithm seems to be more sophisticated.

Translate
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
Contributor ,
Nov 27, 2022 Nov 27, 2022

I know this is two months since you helped me with this, but wanted to thank both of you for your invaluable assistance.  I now have the Clone Case working in my scripts...what a difference.  My last major hurtle is the capture classes.  K.Daube recently posted some very helpful information that I just need to sort out.  

Translate
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
Contributor ,
Dec 03, 2022 Dec 03, 2022
LATEST

Wanted to share a change I made to the Clone Case function you shared.  The docs I work with have a lot of words that are all uppercase, so it tests if the word is all uppercase and converts the "change" string to all uppercase.  Next it tests the first character of the string and converts the first character of the "change" string to uppercase.  If neither condition is met, it returns the change string with no changes.  This allows me to preserve capitalization in my change strings; e.g. it won't convert "Doppler" to "doppler" when I'm looking for capitalization errors.  I rarely need to convert the first character to lowercase, so I took that out.

function getCloneCaseString (find, change) {
    var firstLetter;
    firstLetter = find[0];
     // Check if find string is all uppercase.
     // If true, convert change string to all uppercase.
     if(find === find.toUpperCase()) {
        return change.toUpperCase();
        } else {
            // Get first letter of the find string and check its case.
            if (firstLetter === firstLetter.toUpperCase ()) {
                // If the first letter of the find string is uppercase,
                // convert first letter of the change string to uppercase.
                return change.charAt (0).toUpperCase () + change.slice (1);
              } else {
                // Otherwise, return the change string without any changes.
                return change
              }
       }
}

 

Translate
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