Skip to main content
PetePLTR
Participating Frequently
August 2, 2023
Question

expression selector help...

  • August 2, 2023
  • 2 replies
  • 250 views

Hey, I'm trying to get a text expression selector to just select the last 2 characters or each line in a block of text, this is what I have so far...

 

// Get the total number of characters in the text layer
totalChars = text.sourceText.length;

// Replace line breaks with a consistent character
textContent = text.sourceText.replace(/[\r\n]+/g, '\n');

// Get the number of characters in the current line
currentLine = textContent.slice(0, textIndex).split("\n").length;
lineStartIndex = textContent.lastIndexOf("\n", textIndex - 1) + 1;
lineEndIndex = textContent.indexOf("\n", textIndex);
if (lineEndIndex == -1) lineEndIndex = totalChars;

// Calculate the position of the last two characters in the current line
lastCharPos = lineEndIndex - lineStartIndex - (currentLine - 1);
secondLastCharPos = lastCharPos - 1;

// Check if the current character is one of the last two characters in the line
if (textIndex - lineStartIndex == secondLastCharPos || textIndex - lineStartIndex == lastCharPos) {
    100;
} else {
    0;
}

 

which works for the first 12 lines (I have 16 lines in total), then stops working?

 

Any ideas why its failing aster 12 lines?

 

Ta

 

Pete

This topic has been closed for replies.

2 replies

Dan Ebberts
Community Expert
Community Expert
August 2, 2023

Try this:

txt = text.sourceText.split("\r");
totLen = 0;
val = 0;
for (i = 0; i < txt.length; i++){
  totLen += txt[i].length;
  if (textIndex <= totLen && textIndex > (totLen-2)){
    val = 100;
    break;
  }
}
val
Mylenium
Legend
August 2, 2023

textIndex is a reserved keyword. You cannot use it arbitrarily in your own code out of context. It will honor the setting of the selector (by word, by line, by character) and it seems you expect it to automatically switch to counting the intra-line characters after having separated the lines. This is bound to cause some sort of conflict and it may just work accidentally. You need to rename one of your variables.

 

Mylenium