Roll20 uses cookies to improve your experience on our site. Cookies enable you to enjoy certain features, social sharing functionality, and tailor message and display ads to your interests on our site and others. They also help us understand how our site is being used. By continuing to use our site, you consent to our use of cookies. Update your cookie preferences .
×
Create a free account

string.match( regular expression )

July 11 (6 years ago)
WurmD
KS Backer

Hi, I'm trying and failing to find the proper sintax to make the above work.

The correct regular expression is: \(\d+\ turns\)  https://regex101.com/r/FIZuRd/1

But in the API editor

        var numberStr = str.match('/\(\d+\ turns\)'); -> numberStr = undefined

        var numberStr = str.match(/\(\d+\ turns\)); -> SyntaxError: Invalid regular expression: missing /

        var numberStr = str.match(\/\(\d+\ turns\)); -> SyntaxError: Invalid or unexpected token

Does someone know how to overcome this hurdle?
Thank you



July 11 (6 years ago)

Edited July 11 (6 years ago)
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator

In Java script, regex expressions are surrounded by forward slashes (/). You're problem is you haven't closed off the regex or have enclosed the regex  in quotes making the whole thing (including the regex declaring forward slashes) a string. In the API script editor, you might notice that your entire line is yellow after the start of your regex. This is the syntax you want:

str.match(/\(\d+ turns\)/);

This will match the text (4 turns). If you want to use regex flags (e.g, global), add them after the closing forward slash:

str.match(/\(\d+ turns\)/igm);

Note that I removed an escape you had in there for your space. There's no real reason to escape a basic space. What you could do though is look for any number of whitespace to make it more flexible:

str.match(/\(\d+\s+turns\)/igm);


Edit: also, I'm not sure what your exact goal is for this match is, but I've found that .match is not terribly useful. If testing for text in a string I find /\(\d+\s+turns\)/igm.test(str) much more useful. If you're looking to replace or iterate through matches and do something with them, then I typically like to use str.replace(/\(\d+\s+turns\)/igm,""). You can use a callback function instead of the string in the second argument of .replace.

July 12 (6 years ago)

Edited July 12 (6 years ago)
WurmD
KS Backer

Thanks! That was instrumental in I succeeding (in a very rough and tumble way):

    reduceUncroakedTurns = function(str) {
        var numberStr = String(str.match(/\(\d+ turns\)/));
        var justNumberStr = numberStr.match(/\d+/);
        if(justNumberStr != null){
            var number = parseInt(justNumberStr.map(Number));
            var reducedStr = str.replace(/\(\d+\s+turns\)/,'(' + String(number-1) + ' turns)');
            return reducedStr;
        }
        else
            return str;            
    },
This expects the string to have zero, or 1 and only 1 "...(X turns)..." that I reduce X by 1
But there may be other numbers in the whole string, so \d+ wasn't enough

Thanks again!
July 12 (6 years ago)

Edited July 12 (6 years ago)
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator

Glad it's working for you. There are some improvements that you could make with your code if you want them:

reduceUncroakedTurns = function(str) {
    return str.replace(/(\()(\d+)(\s+turns\))/,(match,begin,number,end)=>{
        return `${begin}${number*1 - 1}${end}`;
    });
},

The replace checks for the regex (replicating your initial .match). Using groups in the regex allows us to separate the match into the various parts that will be needed for manipulation. The callback function allows us to do more complicated parsing of the string than just using a string as the second argument. The function only fires if the match is found and if the match is not found then the string is returned unmanipulated.