Status
Not open for further replies.

Mark91

Veteran
Veteran
Joined
May 19, 2017
Messages
155
Reaction score
18
First Language
Italian
Primarily Uses
RMMV
Hi, dear forum,
I need to check for a number in a string and then put that number into another variable.
I tried using this script:

Code:
var num = parseInt($gameVariables.value(27).match(/\d{4}/)[0]);
$gameVariables.setValue(184, (num>=1000 && num <= 9999) ? num : 0);

Variable 27 is where the string is stored, and variable 184 is the one where the number should be inserted.

It works, but it seems to check only for a 4 digit number (or \d{3} or \d{2}.........).
Now, basically, I need to upgrade this code so it can check for any kind of number (without considering its length) in the string.
Is that possible?
 

Featherbrain

Prehistoric Gamer
Veteran
Joined
Jan 12, 2020
Messages
164
Reaction score
386
First Language
English
Primarily Uses
RMMV
Try this:

JavaScript:
// Basic example:
var txt = "getnumber1234-and-5";
var num = txt.replace(/[^0-9]/g,'');
console.log(num);
// Expected output: "12345"

// RPG Maker version:
var txt = $gameVariables.value(27);
var num = txt.replace(/[^0-9]/g,'');
$gameVariables.setValue(184, num);
 

Nolonar

Veteran
Veteran
Joined
Feb 18, 2018
Messages
273
Reaction score
376
First Language
French, German
Primarily Uses
RMMZ
It works, but it seems to check only for a 4 digit number (or \d{3} or \d{2}.........).
Now, basically, I need to upgrade this code so it can check for any kind of number (without considering its length) in the string.
Is that possible?

Yes, you're looking for the pattern \d+, which matches at least 1 digit.
Other patterns include:

  • \d{1,3} matches at least 1 digit, but no more than 3 digits.
  • \d{2,} matches at least 2 digits.
  • \d{,4} matches no more than 4 digits.
  • \d{5} matches exactly 5 digits (what you are currently using).
  • \d? matches a digit if it exists (equivalent to \d{0,1}).
  • \d* matches any number of digits, including no digits (equivalent to \d{0,}).
 

Featherbrain

Prehistoric Gamer
Veteran
Joined
Jan 12, 2020
Messages
164
Reaction score
386
First Language
English
Primarily Uses
RMMV
Using that method you also need to change the second line, because it's checking whether num is a 4-digit number (in the range of 1000-9999) and setting the game variable to 0 if it isn't.

Try this, which should set the variable to the value of num if it exists, or 0 if it doesn't (e.g. if no digits were found in the original string).

$gameVariables.setValue(184, num || 0);
 

Mark91

Veteran
Veteran
Joined
May 19, 2017
Messages
155
Reaction score
18
First Language
Italian
Primarily Uses
RMMV
Thank you both! The problem is solved!^^
 

Shaz

Global Moderators
Global Mod
Joined
Mar 2, 2012
Messages
43,720
Reaction score
15,304
First Language
English
Primarily Uses
RMMV

This thread is being closed, due to being solved. If for some reason you would like this thread re-opened, please report this post and leave a message why. Thank you.

 
Status
Not open for further replies.

Latest Threads

Latest Profile Posts

does anyone feel like explaining to me the difference between simulation and emulation? you have my full permission to be as pedantic as you want.
About the Deer, I had to cut some piece of video due the *swearing* Youtube copyrights issues...
A caveman pokes his head in...
I was listening to songs by Chuck E Cheese's voice actor (Jaret Reddick from Bowling for Soup) when the meds I was taking for my stomach virus apparently stopped working. I can't take my next dose until morning, but it's no use anyway. I'm officially sick again. Of course, I'll definitely take the next dose either way, but it's useless now.
New profile picture! This time, it's representative of my current project.

Forum statistics

Threads
116,005
Messages
1,094,836
Members
151,310
Latest member
adultcart1
Top