Status
Not open for further replies.

Mark91

Veteran
Veteran
Joined
May 19, 2017
Messages
152
Reaction score
17
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
160
Reaction score
378
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
263
Reaction score
367
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
160
Reaction score
378
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
152
Reaction score
17
First Language
Italian
Primarily Uses
RMMV
Thank you both! The problem is solved!^^
 

Shaz

Global Moderators
Global Mod
Joined
Mar 2, 2012
Messages
43,419
Reaction score
15,214
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

seems absolutely WILD to me now all the hundreds of hours of work I used to do in 2k(3) over a decade ago with a resolution of 320x240. that seems so insanely TINY now, seemed so normal at the time!
Playing Aroma Indosiar theme song.
Aroma is an Indonesian TV show about cooking. :)
Working on a characteeer! (Why is it so hard to color hair?) ;-;
Silly-Sketches.png
Just finished eventing a couple simple puzzles in RMMV, for my very first game "Sample Quest." Just messing around, throwing things together, and calling it a story XD On a non-RM note, got my hair cut today!
I feel the need to inform everyone on the forums NOT to go with Web Hosting Canada for your hosting needs. It is by far the worst customer service and just overall experience I have ever had with hosting and I am Canadian...

Forum statistics

Threads
115,372
Messages
1,089,347
Members
150,067
Latest member
GutterRat
Top