-
Break String into Substrings if true, once true stop checking
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 ;