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

[JS] Capture last 2 characters with: textRange or contents

LEGEND ,
Sep 17, 2021 Sep 17, 2021

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 ]

TOPICS
Scripting
779
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

Guide , Sep 17, 2021 Sep 17, 2021

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. 

Translate
Community Expert , Sep 18, 2021 Sep 18, 2021

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
...
Translate
Adobe
Guide ,
Sep 17, 2021 Sep 17, 2021

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. 

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 18, 2021 Sep 18, 2021

Hi,

you can use textRanges better --> change its length property.

 

I will post an example later.

 

 

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 18, 2021 Sep 18, 2021

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

😉

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
LEGEND ,
Sep 22, 2021 Sep 22, 2021

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

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
Guide ,
Sep 22, 2021 Sep 22, 2021

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

 

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
LEGEND ,
Sep 19, 2021 Sep 19, 2021

Thank you both! I wondered if RegEx was a better option. It does seem a little more direct.

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, 2021 Sep 22, 2021
LATEST
pixxxelschubser_1-1632349428984.png

@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

 

 

 

 

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