Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Forums Adobe After Effects Expressions Eliminate line breaks with regex!

  • Eliminate line breaks with regex!

  • Jonatan Magrao

    January 10, 2021 at 12:09 am

    Hello everyone!

    I’m need a help to “eliminate” the line breaks character count from an expression i’m doing.

    The expression reveals the position of the first charactere of a word i set to encounter, and sum with

    the word length to reveal the last character position.

    t = text.sourceText; //text

    w = thisComp.layer("word").text.sourceText; //word do find

    IN =; //first word's character positon

    OUT = IN + w.length; // last word's character position

    The main problem is that in this way, when the text have multiples line breaks, it sum a number into my calculation.

    That way, as lower line the word is in, it lose the right position.

    I would like to set a regex that cut out the line break character count, or at least encounter the line position of the word to substract from my math. eg. If the word is in the line 1, nothing is needed; if the word is in the line 2, substract 1 from IN; if the word is in the line 3, substract 2 from IN and so on.

    Thanks in advance!

  • Fabrice Leconte

    January 10, 2021 at 3:11 am

    Hello, maybe something like this:


    OUT -= t.substr(0,OUT).match(/\n/g).length;


  • Jonatan Magrao

    January 10, 2021 at 8:36 pm

    Hello Fabrice!

    Unfortunatelly i wasn’t able to set your expression (i’m not a developer, so some things are still hard to me)! I figured out, with some help, a solution, but it’s not perfect, because now, if i choose a word in the begining of the line, it count another character that isn’t selected, but it works better than my own version solo ^^ And i learned how to use substr() method i didn’t knew, and for sure i’ll use it in other projects =D

    I’ll send a image with the full expression, and a link for the mogrt shared (to use in premiere pro). If you or other find a way to fix this little issue, i would be very greatefull.

    Nenhuma descrição de foto disponível.

  • Fabrice Leconte

    January 11, 2021 at 1:11 am

    Try this:

    var w = thisComp.layer("word").text.sourceText;

    var re = new RegExp(w,"g");

    txt = text.sourceText.replace(/\r?\n|\r/g,'').replace(re,function($0){

    return '~'.repeat($0.length)


    if(txt.charAt(textIndex - 1)== '~'){100}else{0}

  • Andrei Popa

    January 12, 2021 at 8:08 am

    If you want to try an alternate solution, I would change the “based on” from the expression selector to “Words” and use this expression. It’s a bit simpler and also works if your text contains “~”

    txt = text.sourceText.replace(/\r/g,” “).split(” “);

    word = thisComp.layer(“word”).text.sourceText;

    if(txt[textIndex-1] == word) {100} else {0}

  • Jonatan Magrao

    January 12, 2021 at 5:26 pm

    Thank you Fabrice, it worked perfectly, the only problem is that it select the same word everytime, so if i want to select only one word, i don’t know how to proceed. Also, i have another question: what if i want select only the second “erat”, and not the first or the third?

    Sorry to boring, my regex is the worse ^^’

  • Jonatan Magrao

    January 12, 2021 at 7:06 pm

    Hey, really thank you Andrei! It’s a way simple, and i thing i can understand yours hehe the main problem is that select only one word and the same problem as the Fabrice: it select all the words if alone! I think it’s simple to change for you guys who understand that. Also, i would like to increment it, if it’s possible. Using the old and ugly code of mine (sorry bout it), and following a pal in facebook group (that i think you’re in too), trying to avoid errors in writing the selected word or sentence to highlight, maybe surround the word or sentence with a special character, like in the image could help. It works better, i think and avoid re-write the thing and errors.

    Following the image, there are 2 layers: one with the original text and the second as a “container” that will show us the final result.
    The first break the text in 3 parts, wich the second one is inside the special character (in this case, “//”).

    Now, it works with one single word and a full sentence, but not with a multiline sentence and it give an error (i didn’t tested in premiere pro yet). It must have a better way to make this work. Also, if could make the sourceText.split code better, it would be awesome too!!

Viewing 1 - 7 of 7 posts

Log in to reply.

We use anonymous cookies to give you the best experience we can.
Our Privacy policy | GDPR Policy