Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Change Color of text inside parenthesis

  • Change Color of text inside parenthesis

    Posted by Raj Sha on July 11, 2019 at 5:15 pm

    Hello Everyone,

    This might sound simple to you, but I have 0 knowledge of expressions.

    I am trying to find a way to change color of a certain text in text box.

    Anything inside parenthesis should be of yellow color and rest of the color should be default white.

    For Ex. 1. (adj) Not being in a specified place
    2. (v) Go away or leave

    So in above example, I need to color (adj) and (n) leaving everything else same.

    Please help

    Thanks,
    Raj

    Andrew Brinkmann replied 4 years, 6 months ago 3 Members · 15 Replies
  • 15 Replies
  • Andrei Popa

    July 12, 2019 at 7:20 am

    I’m not that good with regular expressions, but i remembered this post of Oleg Pirogov and after few searches on Stack Overflow i came up with this. Worked on my test. I used two occurences, one with space and one without.

    So add a text animator, fill color, rgb. To this animator add selector>expression. In the expression field write this:

    var t = text.sourceText;
    var regex = /\(([^)]+)(?=\))/g;

    function indexMatched(index){
    var matched = false;
    var match;
    var i=[];

    while ((match = regex.exec(t)) !== null){
    matched |= match.index

    Andrei
    My Envato portfolio.

  • Andrei Popa

    July 12, 2019 at 11:48 am

    Without the i part…

    var t = text.sourceText;
    var regex = /\(([^)]+)(?=\))/g;

    function indexMatched(index){
    var matched = false;
    var match;

    while ((match = regex.exec(t)) !== null){
    matched |= match.index

    Andrei
    My Envato portfolio.

  • Raj Sha

    July 12, 2019 at 1:10 pm

    Thank You for taking time out and replying Andrei.

    I tried your code and it gives me this error.

    Can you help?

  • Andrei Popa

    July 12, 2019 at 1:43 pm

    I am pretty sure you put the expression where it was not supposed to be.

    Here is a short screen rec.
    13527_tutorial.mp4.zip

    Andrei
    My Envato portfolio.

  • Raj Sha

    July 12, 2019 at 2:07 pm

    Thank You Andrei!!

    You were correct, I was pasting the expression at some place else.

    So this fixes the problem for first occurrence of parenthesis. But for second parenthesis, it moves the red color to the next character and then to next character if there are more parenthesis.

    Attaching Screenshot.

  • Andrei Popa

    July 12, 2019 at 2:21 pm

    That is because the amount selector does not count the enters at the end of the line. This works for multiple paratheses, as long as they are only on the first line or in a textbox. I can’t think of a solution right now. The best i can come up with is split into multiple layers so you don’t need to use enter before any paratheses.

    Andrei
    My Envato portfolio.

  • Raj Sha

    July 12, 2019 at 2:37 pm

    Thank You Andrei for all your support and help!! ☺

    I will wait for someone to reply, if they can come up with any solution or will split my text into multiple layers.

    Thank You once again ☺
    Rajat

  • Andrei Popa

    July 12, 2019 at 2:43 pm

    Nvm, i got this. The kind of problem that does not go out of your head…

    var t = text.sourceText;
    var splitText = t.split("\r");
    var regex = /\(([^)]+)(?=\))/g;

    function indexMatched(index){
    var matched = false;
    var match;
    while ((match = regex.exec(t)) !== null){
    matched |= match.index<index & index<match.index+match[0].length;
    }
    return matched;
    }

    function placeInLines(index){
    i=0;
    maxIndex = 0;
    while(index>maxIndex){
    maxIndex += splitText[i].length;
    i+=1;
    }
    return i-2;
    }

    indexMatched(textIndex+placeInLines(textIndex)) ? 100 : 0;

    Still, i think there are better solutions. I would like to see someone solve this in a more elegant manner.

    Andrei
    My Envato portfolio.

  • Raj Sha

    July 13, 2019 at 5:29 pm

    Yay!!

    Exactly what I was looking for.

    Thank You So So Much Andrei. God Bless You!!

    I am pretty sure many more people will be looking for exactly same thing. You have helped us all.

    Thank You once again for all your support and time.

    Cheers!! 🙂

  • Raj Sha

    July 15, 2019 at 3:41 pm

    Hello Everyone,

    I am really sorry, but I forgot to mention that the word inside parentheses should also turn italics?

    Is this achievable?

    Thanks in Advance!

Page 1 of 2

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