Regex Issue...

0 favourites
  • 1 posts
  • So I've been taking a little time to look at Regex in combination with BBCode, and that's proving successful.. mostly... I can get it to work properly about 50% of the time. So there's clearly something I'm missing.

    Example 1:

    hlColor = "red"

    string = "$hexValue"

    regexReplace( Self.Text, "\$\S+", "gi", "[color=" & hlColor & "]$&[/color]" )

    // \$ = find "$", \S+ = include all non-white spaces until reaching a white space

    // "gi" = "g"lobal: fins all matches, "i"nsensitive: ignore upper/lowercase

    Expected Result: "$hexValue" // appears coloured "red"

    Received Result: "$hexValue" // appears coloured "red"

    Example 2:

    hlColor = "red"

    string = "123"

    regexReplace( Self.Text, "\d", "g", "[color=" & hlColor & "]$&[/color]" )

    // \d = find any decimal digit

    Expected Result: "123" // appears coloured "red"

    Received Result: "123" // appears coloured "red"

    So these work as expected, however...

    Example 3:

    hlColor = "red"

    string = "Label:"

    regexReplace( Self.text, "\w+\b:", "gi", "[color=" & hlColor & "]$&[/color]" )

    // \w+ = find entire word, \: = that ends with ":"

    Expected Result: "Label:" // appears coloured "red"

    Received Result: "[color=red]Label:" // BBCode is escaped and printed, uncoloured

    Example 4:

    hlColor = "red"

    string = "%10011001"

    regexReplace( Self.text, "\%\S+", "g", "[color=" & hlColor & "]$&[/color]" )

    // \% = find "%", \S+ = include all non-white spaces until reaching a whitespace

    Expected Result: "%10011001" // appears coloured "red"

    Received Result: "[color=red]%10011001" // BBCode is escaped and printed, uncoloured

    So obviously these do not work, but I can't figure out why. All I can think is that there's a sneaky "\" being added somewhere that is causing the first half of the BBCode to escape and be treated as plain text. Unless I'm missing something...

    In terms of match selection, everything seems to be working properly. I've also double checked the selections here https://www.regexpal.com/

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)