A library of After Effects expressions to be used for sourceText property of a Text Layer.
Repetition
Repeat input string
n =10; // number to times to repeatvalue.repeat(n)
var n =8; // number of times to repeats = myText = value;for(i=1; i<n; i++) s += myText; s;
n =8; // connect to a slider s = myText = value;for(i=1; i<n; i++) s +="\r"+ myText; s;
Text Grid - Uniform Repetition
Explicit
var s = value; // Source textvar numColumns =5; // Number of columnsvar numRows =10; // Number of rowsvar row =s.repeat(numColumns) +"\r"; row.repeat(numRows);
Concise
(value.repeat(5) +"\r").repeat(10);
// User Input:var numRows =10; // Number of rows in the outputvar numColumns =22; // Number of columns in the outputvar s = value; // Enter the value to be repeated in each cell// Seedvar result ="";var j =0;while (j < numRows) {var k =0;while (k < numColumns) { result += s; // Placeholder for the actual random alphanumeric generation code k +=1; } result +="\r"; j +=1;}result
Text Grid - Multiple Line Repetition
This code splits a comma-separated string into an array, then formats it into a text block with rows and columns by repeating each item with added prefixes and suffixes.
conststr= value;conststrArr=str.split(",");constprefix=""; constsuffix=" ";constnumColumns=5;constnumRows=10;let result ="";for (let i =0; i < numRows; i++) {consteleIndex= i %strArr.length; result += (prefix+strArr[eleIndex]+suffix).repeat(numColumns) +"\r"; }result;
This code creates a string of sequential numbers between a start and end range. The numbers are arranged in rows and columns, and each number can have leading zeros and spaces after it.
//user variablesvar startNum =0;var maxNum =200;var numCol =10;var numZero =2;var numSpace =1;var s ="";for (var i =1; i <= maxNum; i++) {// add startNum var n = startNum + i -1;// add zeroesfor (var j =1; j <= numZero; j++) {if (n <Math.pow(10, j)) s +=0 }// add number to string s += n;// add spaces for (var j =0; j < numSpace; j++) s +=' '// add line break i % numCol ==0? s +='\n': s}
Text Grid - Random Letter
This function generates a grid of random letters, and the user specify the total number of letters and letters per row.
functiongenLetter() { r =random(65,90); //from a to zreturnString.fromCharCode(r);}functionrandAlphabet(maxNum, col, seed) {seedRandom(seed,true);var s ="";for (i =1; i <= maxNum; i++) { s +=genLetter() +' ';if (i % col ==0) s +='\n'; // add line break }return s}randAlphabet(500,25,1)
Text Grid - Random Binary
functionbinary(maxNum, col, seed) {seedRandom(seed,true);var s ="";for (var i =1; i <= maxNum; i++) { s +=Math.round(random()) +' ';if (i % col ==0) s +='\n'; }return s;}// Usagebinary(500,25,1);
n =300; // connect to sliderseedRandom(seed,true);let m = b ='';for(i=0;i<n;i++){seedRandom(i+seed,true); b =Math.round(random(1)).toString() m +=b;}
Randomization
Random Letter
// User Inputvar numOfLetters =10; // Modify the number of letters to generatevar useSpaces =true; // Modify to include spaces between letters (true) or not (false)var changeEveryFrame =false; // Modify if the generated letters should change every frame// Don’t modify below this lineseedRandom(index,!changeEveryFrame);functiongenLetter() {var r =random(65,90); // Generate a random number representing a letter (ASCII code for A to Z)returnString.fromCharCode(r); // Convert the number to the corresponding letter character}var result ="";for (var i =0; i < numOfLetters; i++) { result +=genLetter();if (useSpaces) { result +=" "; // Add a space between letters if useSpaces is true }}result; //source: https://creativecow.net/forums/thread/random-letters-4/
// Number of rows in the outputvar numRows =1;// Number of characters in each row of the outputvar numChars =5;// Number of frames to hold the same random seedvar holdFrames =5;// Calculate the seed for randomization based on time and holdFramesvar seed =Math.floor(time / (holdFrames *thisComp.frameDuration));seedRandom(seed,true);var result ="";var j =0;while (j < numRows) {var k =0;while (k < numChars) {var randomCharCode =Math.floor(random(65,91)); // Generate random number representing uppercase letter in ASCII result +=String.fromCharCode(randomCharCode); // Convert random number to corresponding letter character k +=1; } result +="\r"; // Add a carriage return at the end of each row j +=1;}result; // Output the generated string
Random Digit
seedRandom(10,true);Math.round(random(9));
String Manipulation
Search and replacing string instances
var searchString = /\r/g; // \r: lien break; \g: global flagvar replaceString ="newString";value.replace(searchString, replaceString);
Select text indexes explicitly; e.g. 1, 3, 7, 14, 22, etc.?
If you don't have a lot of numbers, then this works:if(textIndex ===1|| textIndex ===3) {100;} else {0;}If you do have a ton of numbers, then this works:i = [1,3,5,7,10,11];inArray(i);functioninArray(obj) { b =0;for(i =0; i <obj.length; i++) {if(obj[i] === textIndex) { b =100;break; } }return b;}
// getting the character countl =thisComp.layer("your text layer").text.sourceText.length;// getting word count l =thisComp.layer("your text layer").text.sourceText;wordsCount =l.split(/ ,|| \./).length;// Matching text with regexthisComp.layer("text").text.sourceText.toLowerCase().match(/\w+\./))// includes() — determines whether a string contains the characters of a specified strinvar str ="Hello world, welcome to the universe.";var n =str.includes("world");
Utility Functions
Find Number of Layers that includes string
functionfindLayerbyName(str) {var n =thisComp.numLayers;var numMattes =0;for (i =1; i <= n; i++) {thisComp.layer(i).name.includes(str) ==1? numMattes +=1:0; }return numMattes}findLayerbyName("matte")