Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Break String into Substrings if true, once true stop checking

  • Break String into Substrings if true, once true stop checking

    Posted by Bryce Poole on October 10, 2014 at 4:11 pm

    Hello,

    I am trying to write a bit of code that will search a users input string for a particular set of characters, and if found break the string at that point into a substring. I have some code that seems to be doing what I want, however, I need to be able to add the set of characters again in string two without it moving the split point, currently my code breaks if the input is entered twice.

    For example have the string “go [up] or [down]” break into two substrings (go [up]) (or [down])

    At the moment my code is able to break the string apart at the first instance of [direction] and two substrings exist, however as soon as a second direction is entered into the substring the code breaks and it goes back to just one string.

    I need some way to make the ternary statement stop checking once it is true so the input can be entered again without changing the substring split point, however I’m not quite sure how to write this. Would anyone be able to help me out with figuring this out?

    //define variables
    var txtIn = thisComp.layer("Add Text Here").text.sourceText;
    var x;

    /*
    ternary statement to look for arrow direction input,
    if found breaks string at that location, otherwise
    returns max string length of 9999
    */

    var x = txtIn.match(/\[(up|down|left|right)\]/) === null ? 9999 : txtIn.indexOf(txtIn.match(/\[(up|down|left|right)\]/)[0]);

    //breaks string apart from start to x;
    var string1 = txtIn.substring(0, x );

    string1 ;

    Bryce Poole replied 11 years, 9 months ago 2 Members · 5 Replies
  • 5 Replies
  • Xavier Gomez

    October 10, 2014 at 7:52 pm

    Hi,

    here is a possibitity, not sure it is the most efficient one though.
    It adds a delimiter tag after each of these patterns, then splits the string:

    var textIn = “go [up] or [down] then [right] but not [left]”;
    var D = “#”; // or something else
    var strings = textIn.replace(/(\[(up|down|right|left)\])\s*(?!$)/g, “$1″+D).split(D); // an array of substrings of textIn

    Xavier

  • Bryce Poole

    October 10, 2014 at 8:42 pm

    Hey Xavier,

    Thank you for the response, I think this might work to solve my problem! This seems to work well as long as the first [direction] is present, but sometimes the string may not contain any directions at all, in which case I’d like to keep it whole. Would it be possible to add some type of null catch for when there isn’t a second string present? Ideally I’d like to set the string to a very long max value as a default, something like 9999.

    For example:
    “go [up] first” or “go [up] then [down]” both work but “go wherever” returns an error when calling the second index in the string.

    var textIn = thisComp.layer("Add Text Here").text.sourceText;
    var D = "#"; // or something else
    var strings = textIn.replace(/(\[(up|down|right|left)\])\s*(?!$)/g, "$1"+D).split(D); // an array of substrings of textIn
    strings[1];// returns error if string does not contain [up], [down], [left], or [right]

  • Bryce Poole

    October 10, 2014 at 8:47 pm

    I think I may have just solved it by adding a simple if statement to the declaration of the strings array.
    Though there might still be a better way to do this? Thank you again for all the help!

    var textIn = thisComp.layer("Add Text Here").text.sourceText;
    var D = "#"; // or something else
    var strings = textIn.replace(/(\[(up|down|right|left)\])\s*(?!$)/g, "$1"+D).split(D); // an array of substrings of textIn

    if (textIn.match(/\[(up|down|left|right)\]/) ) {
    strings[1];
    }else{
    strings[0];
    }

  • Xavier Gomez

    October 10, 2014 at 9:04 pm

    With this expression, the entries of the array ‘strings’ are all of the formm “something+[up/down/left/right]”, except maybe the very last one. So for that last entry you have to check again:

    N = strings.length;
    all entries: strings[n] (n=0,…., N-2) have one of your direction patterns at the end;
    the last entry (which is strings[0] if N===1) can be an exception:

    if (strings[N-1].match(/\[(up|down|left|right)\]/)){
    doThis; // direction spotted
    }
    else{
    doThat; // no direction spotted
    };

    But… there is maybe another solution that leads more straigthforwardly to the same thing.

    //===================

    Edit: actually if you remove the (?!$) in the regexp (which tells not to match if the [up/down] pattern if right at the end of the string, i added this so you dont end up with an empty entry) then the last entry of string NEVER has a direction and all other have one:

    strings = textIn.replace(/([(up|down|right|left)])s*/g, “$1″+D).split(D);
    N = strings.length;

    //>>> now all entries strings[n] (n=0,… N-2) have a direction, the last one not (and can be an empty string). Maybe better that way.

  • Bryce Poole

    October 19, 2014 at 4:24 pm

    Thank you Xavier! This solved it!

    Though, I’m not totally sure I understand what all the RegExpr parts are doing, if you have time would you mind breaking it down for me, just so I can fully wrap my brain around this?

    s*/g, “$1″+D).split(D); //specifically this bit

    In either case this totally solved it, thank you very much!

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