Copy link to clipboard
Copied
Something alludes me:
I want to capture a "range" of characters from a text frame as a variable (in this case I want the last 2 characters).
How do I do that using textRange and/or contents?
[ branched from Textranges vs TextSelection to Illustrator forum by moderator ]
One way:
var textFrame1 = app.selection[0]
var length = textFrame1.textRanges.length;
var last = textFrame1.textRanges[length-1];
var beforeLast = textFrame1.textRanges[length-2];
alert( beforeLast.contents+last.contents );
You can also use string functions on the contents.
Here is the example:
// [JS] Capture last 2 characters with: textRange or contents
// https://community.adobe.com/t5/illustrator-discussions/js-capture-last-2-characters-with-textrange-or-contents/m-p/12390176#M291706
// catch the last two characters with textRange
// select a textFrame before running this snippet
// regards pixxxelschubser
var howMany = 2; // here you can change the desired number of last characters
var chars = app.selection[0].textRange.characters;
var lastChars = chars[c...
Copy link to clipboard
Copied
One way:
var textFrame1 = app.selection[0]
var length = textFrame1.textRanges.length;
var last = textFrame1.textRanges[length-1];
var beforeLast = textFrame1.textRanges[length-2];
alert( beforeLast.contents+last.contents );
You can also use string functions on the contents.
Copy link to clipboard
Copied
Hi,
you can use textRanges better --> change its length property.
I will post an example later.
Copy link to clipboard
Copied
Here is the example:
// [JS] Capture last 2 characters with: textRange or contents
// https://community.adobe.com/t5/illustrator-discussions/js-capture-last-2-characters-with-textrange-or-contents/m-p/12390176#M291706
// catch the last two characters with textRange
// select a textFrame before running this snippet
// regards pixxxelschubser
var howMany = 2; // here you can change the desired number of last characters
var chars = app.selection[0].textRange.characters;
var lastChars = chars[chars.length-howMany];
lastChars.length = howMany; // set the new textRange.length
alert(howMany+" last character(s): "+lastChars.contents);
But I prefer a different method:
// select a textFrame before running this snippet
var searchString = /(..)$/;
var aTF = app.selection[0];
var foundString = aTF.contents.match (searchString);
alert(foundString[0]);
If that works for you
have fun
😉
Copy link to clipboard
Copied
@pixxxelschubser , I do have a followup question:
When I use the RegEx method it produces an array with 2 identical objects. I saved "foundString[0]" into its own variable. Why does this method created 2 instances of the same matched string?
Copy link to clipboard
Copied
If I can make an unsolicited comment while you're waiting for a reply: Presuming one match, match() returns an array, in which the first element is the whole matched expression and the second element is a parenthesised subexpression (called a capturing group). If the whole expression is parenthesised, both elements will be the same.
alert( "Blue".match(/(B)lue/)[0] ); // Blue
alert( "Blue".match(/(B)lue/)[1] ); // B
alert( "Blue".match(/(Blue)/)[0] ); // Blue
alert( "Blue".match(/(Blue)/)[1] ); // Blue
Copy link to clipboard
Copied
Thank you both! I wondered if RegEx was a better option. It does seem a little more direct.
Copy link to clipboard
Copied
@pixxxelschubser , I do have a followup question:
When I use the RegEx method it produces an array with 2 identical objects. I saved "foundString[0]" into its own variable. Why does this method created 2 instances of the same matched string?
By @rcraighead
Ouch!
This is an extremely extensive area with which one could fill many books. Depending on how the regular expression (Regex) is structured, one achieves completely different results. Only by using wildcards, flags, capturing and non-capturing groups you can exploit the possibilities of Regex.
In this case, @femkeblanco is absolutely right - the match command finds something, and an array is created with the entire find and the first (there is only one here) find (capturing group).
Here are some more basic possibilities that already significantly extend the example he gave. And this is only a small start ...
$.writeln( "Blue".match(/(B)lue/)[0] ); // Blue
$.writeln( "Blue".match(/(B)(l)(u)(e)/) ); // Blue,B,l,u,e
$.writeln( "Blue".match(/(B)(l)(u)(e)/)[0] ); // Blue
$.writeln( "Blue".match(/(B)(l)(u)(e)/)[4] ); // e
$.writeln( "Blue".match(/./g) ); // B,l,u,e
$.writeln( "Blue".match(/./g)[0] ); // B
$.writeln( "Blue".match(/./g)[3] ); // e
$.writeln( "Blue".match(/./) ); // B
$.writeln( "Blue".match(/.$/) ); // e
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more