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
264
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,451
Reaction score
15,229
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 Posts

Latest Profile Posts

Day 2 of my redesign journey, as a follow up from the previous post :kaothx:
pXiRJSm.jpg

This is Cedric's son, Apollo!
New pet peeve: Telling tech support personnel what something is doing, only for them to tell me (in excessively condescending tones) what it should be doing. Yes, I know what it should be doing, and if it was doing that, I wouldn’t be talking to you and your attitude problem about it right now, would I? >.<
uglywolf wrote on Khulse's profile.
Hello.
The rules I makes pun about on your last thread is about "Verify Files" before anything else, if the stuff comes from STEAM. Which means, if it could be fixed with just that, there is no need to redownload the whole stuff.
But glad the reinstall worked, hope you enjoy it and your days too.
uh oh, I just tapped into a fresh, deep vein of nightmare fuel right here guys:

Forum statistics

Threads
115,430
Messages
1,089,798
Members
150,186
Latest member
DarkPurplePrince
Top