ADOBE AFTER EFFECTS: Forum Expressions Tutorials Creative Cloud

Specific word in paragraph be a different color

COW Forums : Adobe After Effects Expressions

<< PREVIOUS   •   FAQ   •   VIEW ALL   •   PRINT   •   NEXT >>
Liam Campbell
Specific word in paragraph be a different color
on Oct 4, 2017 at 3:46:00 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?


Return to posts index

Dan Ebberts
Re: Specific word in paragraph be a different color
on Oct 4, 2017 at 5:59:56 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



Return to posts index

Liam Campbell
Re: Specific word in paragraph be a different color
on Oct 4, 2017 at 8:28:08 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


Return to posts index


Dan Ebberts
Re: Specific word in paragraph be a different color
on Oct 4, 2017 at 8:57:37 pm

What's the text you're using?

Dan



Return to posts index

Liam Campbell
Re: Specific word in paragraph be a different color
on Oct 4, 2017 at 9:02:15 pm

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


Return to posts index

Dan Ebberts
Re: Specific word in paragraph be a different color
on Oct 4, 2017 at 9:17:57 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



Return to posts index


Liam Campbell
Re: Specific word in paragraph be a different color
on Oct 4, 2017 at 9:20:25 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?


Return to posts index

Dan Ebberts
Re: Specific word in paragraph be a different color
on Oct 4, 2017 at 9:37:05 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



Return to posts index

Scott McGee
Re: Specific word in paragraph be a different color
on Oct 5, 2017 at 7:25:44 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


Return to posts index


Mac Lad
Re: Specific word in paragraph be a different color
on Oct 7, 2017 at 8:29:36 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..?


Return to posts index

Dan Ebberts
Re: Specific word in paragraph be a different color
on Oct 7, 2017 at 3:45:42 pm

Did you add the Expression Selector?

Dan



Return to posts index

Mac Lad
Re: Specific word in paragraph be a different color
on Oct 7, 2017 at 6:22:50 pm

Sorry Dan, i didn't put expression selector...but now its working fine ....just for a query can this be worked for multiple words..?
Is that possible...?


Return to posts index


Dan Ebberts
Re: Specific word in paragraph be a different color
on Oct 7, 2017 at 8:41:19 pm

You could set it up for multiple words like this:


wordArray = ["one","two","three","four"];
words = text.sourceText.replace(/^\s+/, "").split(/\s+/);
val = 0;
for (i = 0; i < wordArray.length; i++){
if (words[textIndex-1] == wordArray[i]){
val = 100;
break;
}
}
val



Dan



Return to posts index

Liam Campbell
Re: Specific word in paragraph be a different color
on Oct 7, 2017 at 7:28:10 pm

The only part of this that I don't understand is the [textIndex -1] what exactly is this searching for?


Return to posts index

Dan Ebberts
Re: Specific word in paragraph be a different color
on Oct 7, 2017 at 8:47:56 pm

words is an array of the words in your text, so it is zero-based (indexing starts with zero). textIndex is the word index into the text itself. It starts with one, so you have to subtract one to retrieve the appropriate word from the array. You wouldn't have to go through all this if you could use textIndex directly to access the text that it's indexing.


Dan



Return to posts index


Mac Lad
Re: Specific word in paragraph be a different color
on Oct 8, 2017 at 7:07:46 am

Thanks Dan for awesome help... It also worked with multiple colors by just duplicating Animator and replacing the words from array and changing color parameter


Return to posts index

<< PREVIOUS   •   VIEW ALL   •   PRINT   •   NEXT >>
© 2017 CreativeCOW.net All Rights Reserved
[TOP]