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 )

Hi, I'm trying and failing to find the proper sintax to make the above work. The correct regular expression is: \(\d+\ turns\)&nbsp;&nbsp; <a href="https://regex101.com/r/FIZuRd/1" rel="nofollow">https://regex101.com/r/FIZuRd/1</a> But in the API editor &nbsp; &nbsp; &nbsp; &nbsp; var numberStr = str.match('/\(\d+\ turns\)'); -&gt; numberStr = undefined &nbsp; &nbsp; &nbsp; &nbsp; var numberStr = str.match(/\(\d+\ turns\)); -&gt;&nbsp; SyntaxError: Invalid regular expression: missing / &nbsp; &nbsp; &nbsp; &nbsp; var numberStr = str.match(\/\(\d+\ turns\)); -&gt;&nbsp; SyntaxError: Invalid or unexpected token Does someone know how to overcome this hurdle? Thank you
1562851612

Edited 1562852169
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&nbsp; 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.
1562925810

Edited 1562925884
WurmD
KS Backer
Thanks! That was instrumental in I succeeding (in a very rough and tumble way): &nbsp; &nbsp; reduceUncroakedTurns = function(str) { &nbsp; &nbsp; &nbsp; &nbsp; var numberStr = String(str.match(/\(\d+ turns\)/)); &nbsp; &nbsp; &nbsp; &nbsp; var justNumberStr = numberStr.match(/\d+/); &nbsp; &nbsp; &nbsp; &nbsp; if(justNumberStr != null){ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var number = parseInt(justNumberStr.map(Number)); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var reducedStr = str.replace(/\(\d+\s+turns\)/,'(' + String(number-1) + ' turns)'); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return reducedStr; &nbsp; &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp; &nbsp; &nbsp; else &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return str;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; }, 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&nbsp; \d+ wasn't enough Thanks again!
1562943873

Edited 1562943893
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)=&gt;{ 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.