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 .
×

Can Someone Explain the Arguments in Recommended .replace and .match?

When I am having difficulty trying to figure out where my code is going wrong, I sometimes paste it into chatGPT. I hope that's OK, some folks detest AI and I'm not a fan of using it for images, myself. This is for a script I am developing for AD&D 1e for a chat window's button to show a party member's "wealth notes".  strName is the character's name and Wnotes is the wealth notes taken from the character's sheet using: let Wnotes = getAttrByName ( CharID , "wealthNotes" ) || "" ; The recommended lines I am getting include, in various places: let wealthMsg = '!wealthnotesdisplay "' + strName + '" "' + Wnotes . replace( /"/g , '"' ) + '"' ; I know it is trying to replace something  with a quotation mark, but what is the /"/g character(s)? I don't know what the line is trying to replace. Next, in the procedure for !wealthnotesdisplay (as showing above) there are another couple of .replace statements with some different arguments, here is one: let charName = args [ 0 ] ? args [ 0 ] . replace( /"/g , '' ) : "" ; I don't know what any of the arguments in the .replace are. Can anyone tell me? In addition to the arguments, what is the ? immediately following args[0]? Is the colon after the .replace similar to ||? If not, what is it? Just before this line there is the definition of args, is a .match line. I get it is separating the arguments following the !wealthnotesdisplay command, but I normally use a space or sometimes -- to separate my arguments. What is this doing here? let args = msg.content.match(/"([^"]*)"/g) || []; All I can tell is the || sending an empty array is there are no arguments. Thanks for any help.
Here's basically what each of these commands means: .replace(/"/g,'') Replace all double quotes with nothing (remove all double quotes). [The /g means 'globally' or all of the quotes that are found. Without the /g only the first instance would be replaced.]  .replace(/"/g,'"') Replace all double quotes with the text string '"' (which is the html entity replacement for the double quote character). let charName = args[0] ? args[0].replace(/"/g,'') : ""; Make the charName become one of two things. If the 'args' array has some text, make the charName the first thing in the list (javascript starts lists at 0, not 1), but remove all double quotes. Otherwise (if the 'args' array does not have any text), then make the charName an empty field. [A question mark is a Conditional Operator, which is a quick way of creating an "If ... then" statement. The ? is the 'if', and the : is the 'then'.] let args = msg.content.match(/"([^"]*)"/g) || []; Look at the message and find groups of text that are contained within double quotes. Create an array that lists each of those groups of text. 
Jarren said: let args = msg.content.match(/"([^"]*)"/g) || []; Look at the message and find groups of text that are contained within double quotes. Create an array that lists each of those groups of text.  Super helpful on the quote replacement stuff, thank you, Jerren. The latter seems to be an odd thing to do, though. Really appreciate your quick reply, Jerren.
1781015243
keithcurtis
Forum Champion
Marketplace Creator
API Scripter
Jarren said: let charName = args[0] ? args[0].replace(/"/g,'') : ""; Make the charName become one of two things. If the 'args' array has some text, make the charName the first thing in the list (javascript starts lists at 0, not 1), but remove all double quotes. Otherwise (if the 'args' array does not have any text), then make the charName an empty field. [A question mark is a Conditional Operator, which is a quick way of creating an "If ... then" statement. The ? is the 'if', and the : is the 'then'.] Slight clarification, that's more of an if-then- else . Here it is in an expanded format: if ( args [ 0 ]) { charName = args [ 0 ] . replace( /"/g , '' ); } else { charName = "" ; } The original construction is called a ternary operator, (if you want to research further) and functions like this: condition ? valueIfTrue : valueIfFalse
1781024943
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
Keith and Jarren have done a great job explaining what is going on in the generated code, however I'm not sure this generated code is necessary. Specifically: let wealthMsg = '!wealthnotesdisplay "' + strName + '" "' + Wnotes . replace( /"/g , '"' ) + '"' ; I'm not sure why the wealthnotes are getting wrapped in quotes to begin with and why it's necessary to replace the quotes in the wealth notes. If this is for eventual inclusion in a roll button output in a chat menu style interface, the quotes everywhere are a problem.
1781061064
timmaugh
Pro
API Scripter
One other point of clarification: Formations like: /"([^"]*)"/g ...or /"/g ...are regular expressions. Think of regular expressions like a pattern recognition (and return) engine. They're almost their own language/syntax, but you can use them within javascript. You can play with them at regex101.com, but make sure to change the language Flavor (in the left hand list) to "Javascript".
keithcurtis said: The original construction is called a ternary operator, (if you want to research further) and functions like this: condition ? valueIfTrue : valueIfFalse Interesting stuff. I've been an if person since my first CS class in 1981 :-) so tend to lean towards those or switch type functions (in other environments)