0

I need to create an expression that can "Enclose" a substring. The content of that substring can then be taken, modified, and replace the original substring.

For example:

var str:String = "The balloon is <start>red||blue||green<end> and is <start>rather large||very small<end> in size.";

I don't know to create an expression that consumes the content of the string, from Start until it encounters End.

From there, I need to pull a copy of that substring, modify it (In this case, select a random section, which I already know how to do), and then replace this individual substring before going onto the second substring.

Edit:

Thanks to the redirect, I've written a mostly-working function that produces close to the result I'm looking for, the issue comes at the tail end when replacing the substrings. Here's my function:

function substringfunction(texts:String):void {
    var expression:RegExp = /(?<=<start>)(.*?)(?=<end>)/g;
    var arrayResult:Array = texts.match(expression);
    var arrayLength:Number = arrayResult.length;
    var i:Number = 0;
    for(i = 0; i < arrayLength; i++) {
        var arr:Array = arrayResult[i].split("||");
        var choice:String = (arr[Math.floor(Math.random() * arr.length)]);
        trace(choice);
        texts = texts.replace(expression, choice);
    }
    trace(texts);
}

As you'll find, it replaces all expressions with the choice for every cycle, until they eventually become the random choice for the lattermost array row. I attempted to use an expression that eschews the global flag, but that produced the same result, except now only affecting the first substring, so I'm out of ideas.

Any further assistance would be greatly appreciated.

Marohen
  • 3
  • 3
  • Ok, this one is *really* easy. At least *try* something (and show what you tried), otherwise you'll never learn anything about regex if we just give you a working solution. – Lucas Trzesniewski Aug 13 '15 at 06:57
  • The redirect helped me for the most part -- it doesn't help that I have difficulty trying to phrase the goal I'm trying to achieve, so I couldn't find the solution I was looking for -- and the post has been updated with my results thus far... Though, I don't seem to be getting a response to it, and I assume it's fairly straightforward. Should I make a new post? – Marohen Aug 13 '15 at 13:02
  • Good. You almost made it. To make it work properly, use the `replace` function instead of `match`, and pass it a callback for the replacement part. You'll have to tweak your regex like this: `(.*?)`. Use the first capture group instead of the whole match for the split. – Lucas Trzesniewski Aug 13 '15 at 16:02
  • My apologies, I don't quite fully understand what you're suggesting I use as the replacement part in the altered function for the array variable. – Marohen Aug 13 '15 at 21:21
  • I mean use `texts = texts.replace(expression, function(match, choicesString) { var choices = choicesString.split("||"); ...; return choice });` Fill in the rest almost as is, you get the idea. – Lucas Trzesniewski Aug 14 '15 at 10:01
  • Oh... Oh god, I'm about to have a brain aneurysm. Your solution worked, but I have absolutely no idea as to what I'm looking at. The function inside a function I sorta get, but what purposes do the parameters of that function serve -- specifically, 'match'? – Marohen Aug 15 '15 at 04:07
  • Well, this should be explained in the [docs of the replace function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace#Specifying_a_function_as_a_parameter). I linked to MDN but it's the same in ActionScript. The parameters represent the captured substrings, the first one being the whole match. You only need the second one here (the first captured group). – Lucas Trzesniewski Aug 15 '15 at 09:09
  • Thanks for dealing with me. You've been a great help! – Marohen Aug 16 '15 at 04:24

0 Answers0