Skip to main content
Known Participant
February 10, 2017
Question

Select part of point text objects matching a regex expression

  • February 10, 2017
  • 2 replies
  • 3414 views

Hello! I'm looking for a way to run through a bunch of point text objects and select part of the string in each text object based on a regex expression. I found this awesome script that can do a regex find/replace, but my end goal is not to replace, but to apply a given character style to part of the string.

For example, I have two point text objects:

first / second / third

one / two / three

And I want to be able to select "/ second /" and "/ two /" to be able to apply a different character style to that part of each point text object. Also, i'm not sure that you can have parts of two different point text objects selected at the same time, so the script might need to apply the active text style to the matched string parts as it passes through them.

Is this impossible?

Thanks!

This topic has been closed for replies.

2 replies

Larry G. Schneider
Adobe Expert
April 16, 2017

OK, try switching the equals and greater than in the second statement. ( i >= 0 )

Known Participant
April 16, 2017

Hmm, that one is still coming up with 'atf' as 'no such element'

#target illustrator

function test(){

// regex_changeContentsOfWordOrString_RemainFormatting.jsx

// regards pixxxel schubser

var doc = app.activeDocument;

var s = new RegExp("\\d{3}","gi");

var myStyle = doc.characterStyles.getByName("MyStyle");

for ( i = app.activeDocument.textFrames.length; i >=0; i-- ) {

    var atf = activeDocument.textFrames;

    var result, aCon;

    while ( result = s.exec(atf.contents)) {

        try {

            aCon = atf.characters[result.index];

            aCon.length = result[0].length;

            myStyle.applyTo(aCon);

            } catch (e) {};

        }

}

};

test();

Any help is so appreciated! I'm thinking that maybe i'm going about this loop in the wrong way all together..

Silly-V
Brainiac
April 17, 2017

Ah, it's got to be in your for-loop. When counting backwards, make sure to subtract 1 from the .length of the array. For example if you had an array of 5 items, it's length would be 5. But the very last item is going to be at index [4].

pixxxelschubser
Adobe Expert
February 11, 2017
Known Participant
February 15, 2017

Wow! I think that might be the ticket. I'm not having luck making it work though. I think it must be that my regex is incorrect. I'm wondering if you could shed any light on what I'm doing wrong.

#target illustrator

function test(){

// regex_changeContentsOfWordOrString_RemainFormatting.jsx

// regards pixxxel schubser

var doc = app.activeDocument;

var s = new RegExp("[0-9]*$","gi");

var myStyle = doc.characterStyles.getByName("MyStyle");

var atf = activeDocument.textFrames[0];

while (result = s.exec(atf.contents)) {

    try {

        aCon = atf.characters[result.index];

        aCon.length = result[0].length;

        myStyle.applyTo(aCon);

        } catch (e) {};

    }

};

test();

assuming

-I have a character Style setup named "MyStyle"

-I have two point text objects:

            "one / 334 / three"

            "first / 563 / third"

-I have the point text objects selected (or un-selected?)

when I run the script it should set "334" and "563" from whatever the style was to 'MyStyle', correct? The script appears to run correctly without any errors, but does not make the character style change. If I don't have 'MyStyle' setup, i get an error reporting that.

Thank you!

Silly-V
Brainiac
February 15, 2017

Use this site to test your Regex:Regex Tester - Javascript, PCRE, PHP

You will see that your expression "matches 0 characters and therefore matches infinity".

If you only want 3 numbers in a row, you can try this: \d{3}