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,390
Reaction score
15,204
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

Is it weird that i feel overwhelmed from reading codes that i wrote few months ago? Lol.

I dont even know where to continue!:hsad:
Ami
Gosh,I want to Play this Game Again one more again!

Seeing my Own Character & Kannono Once Again!
Tales_of_the_World_Radiant_Mythology_3_JPN_PSP-Coverart.jpg
I've been a bit to depressed to get work done on the game lately. Don't worry, I'll be okay.
LunarFlareStudios wrote on Solar_Flare's profile.
Hey, my name is Lunar Flare Studios, and you're Solar Flare? That's so weird!

Forum statistics

Threads
115,297
Messages
1,088,729
Members
149,940
Latest member
Malca
Top