javascriptregexampscript

Regex to select all spaces between a special enclosure


I am trying to write a regex for Javascript that can select all whitespaces in between AMPScript brackets, the syntax for the Lang is something like this

%%[


set @truth = 'this is amp'

IF @truth == 'this is amp' THEN
set @response = 'amp rocks'
ELSE
set @response = '️...'
ENDIF

]%%

So far, I am able to select all the characters inside the given brackets using this expression:

%%\[(\s|.)*?\]%%

But this selects all the characters inside the enclosure, is there a method I can use to only select spaces/new lines/tabindents only?

Thanks in advance!


Solution

  • I believe this is what you want:

    (?<=%%\[(\s|.)*)\s*(?=(\s|.)*\]%%)
    

    https://regex101.com/r/EWhbuX/1

    Edit

    Here's a breakdown of the regular expression:

    1.

    First we start with a Positive Lookbehind (?<= )

    This will make sure that the pattern that's following this, will be preceded by the pattern inside, but will not be include it in the matches.

    In this case we want our matching pattern to be preceded by %%[ and any other character %%\[(\s|.)*

    So, our resulting code for the Positive Lookbehind is

    (?<=%%\[(\s|.)*)
    

    2.

    Next comes the pattern that we actually want to match after our Lookabehind, and (spoiler alert), before a Lookahead that we'll define later.

    In this case, that's just any whitespace character, so our pattern will be

    \s
    

    (Yes, I just noticed that we don't even need the * in my original answer)

    3.

    Similarly to what we did at the beginning of the expression with the Lookbehind, we now need a Positive Lookahead (?= )

    This is to make sure that our whitespaces will be followed by any character and ]%% (\s|.)*\]%%.

    So this is our resulting Lookahead:

    (?=(\s|.)*\]%%)
    

    4.

    Put everything together and you have your regular expression!

    (?<=%%\[(\s|.)*)\s(?=(\s|.)*\]%%)