Forums › Adobe After Effects › Script Expression, how to add character – \
-
Script Expression, how to add character – \
-
Timur Solomon
May 13, 2022 at 7:10 amHi all.
I am writing a script to add a regular expression to a text layer, but when the script is running, the character – \ disappears into the expression.
textProp.expression = ‘text.sourceText.replace(/\D+/g, “”)’;
In AE I get – text.sourceText.replace(/D+/g, “”)
How to fix it?
-
Timur Solomon
May 13, 2022 at 7:20 amThe issue is resolved, I just added another character – \
textProp.expression = ‘text.sourceText.replace(/\\D+/g, “”)’;
😄
-
Graham Quince
May 13, 2022 at 7:58 amNice one, thanks for posting the solution, it’ll help the next person struggling.
-
Walter Soyka
May 14, 2022 at 1:04 amGlad you found the solution. As for why that’s the solution, it’s because the backslash is an “escape character” in Javascript strings. It’s not taken literally; rather, it escapes the string and indicates the next character should be not be processed literally. You can use it to add special characters to strings.
For example, if you wanted to add a single quote to a string enclosed in your script in single quotes, you’d do it with \’. You can also add a tab with \t, a newline with \n, and, as you’ve noted, a backslash with \\.
Log in to reply.