Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Specific word in paragraph be a different color

  • Specific word in paragraph be a different color

    Posted by Liam Campbell on October 4, 2017 at 3:46 am

    I asked this about a year ago but still haven’t figured out a solution.

    I want to make fake “hacker code” and I want to be able to make the word “class” always be red while everything else is white for example. Is there a way to identify a string of characters in a block of text?

    Arav Roy replied 3 years, 11 months ago 10 Members · 28 Replies
  • 28 Replies
  • Dan Ebberts

    October 4, 2017 at 5:59 pm

    If you add a color animatior, then add an expression selector, change Based On to Words, then something like this for Amount should work:

    words = text.sourceText.replace(/^s+/, “”).split(/s+/);
    if (words[textIndex-1] == “class”) 100 else 0

    This assumes that “class” is surrounded by white space. If that’s not the case, you need a different strategy.

    Dan

  • Liam Campbell

    October 4, 2017 at 8:28 pm

    When I try it, I get an error that reads “Error at line 2. property or method named 37 in Class ‘Array’ is missing or does not exist. It may have been renamed, moved, deleted, or the name may have been mistyped”

    Could you explain what some of the code is doing so I can better understand where this error could be coming from?

    Thanks for your help

  • Dan Ebberts

    October 4, 2017 at 8:57 pm

    What’s the text you’re using?

    Dan

  • Liam Campbell

    October 4, 2017 at 9:02 pm

    I was using random text but I simplified it to “hello class hello” and I was still getting the error.

  • Dan Ebberts

    October 4, 2017 at 9:17 pm

    Ah, OK. Sorry. The s characters in the replace() and split() should each be preceded by a backslash character. Apparently they got stripped from my post. I’ll try it again, but if they don’t show up you’ll have to put them in manually.


    words = text.sourceText.replace(/^\s+/, "").split(/\s+/);
    if (words[textIndex-1] == "class") 100 else 0

    Dan

  • Liam Campbell

    October 4, 2017 at 9:20 pm

    Works perfectly even on multiple lines. What are the various slashes and “s’s” doing and would I only have to change “class” in the expression to change the word?

  • Dan Ebberts

    October 4, 2017 at 9:37 pm

    Those are Regular Expressions (RegExp) which are used for pattern searching within character strings. They are used in other languages as well as JavaScript, so they look strange at first. The one in replace() strips off any white space (blanks, carriage returns, tabs, etc.) before the first word. The one in split() splits the remaining text into an array of words wherever it finds white space.

    And yes, just change the word “class” if you want to highlight something else.

    Dan

  • Scott Mcgee

    October 5, 2017 at 7:25 am

    Cheers Liam for asking and Dan for answering.

    This is exactly what I wanted and it works. With a few alterations. Honestly didn’t think that putting .indexOf at the end of the open square brackets would work…But I gave it a try and it did.

    words = text.sourceText.replace(/^\s+/, “”).split(/\s+/);
    if (words[textIndex-1].indexOf (“@”) >-1) 100 else 0

  • Mac Lad

    October 7, 2017 at 8:29 am

    I tried pasting this expression in amount but that didn’t work for me

    I created a text layer (hello class hello)> added a color>RGB animator
    changed Based on to Words and added expression in amount

    am I doing something wrong..?

  • Dan Ebberts

    October 7, 2017 at 3:45 pm

    Did you add the Expression Selector?

    Dan

Page 1 of 3

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