RMMV How to count characters occurrences in a string?

Mark91

Veteran
Veteran
Joined
May 19, 2017
Messages
155
Reaction score
18
First Language
Italian
Primarily Uses
RMMV
Hi^^
I'm trying to count how many occurrences of a character (especially punctuation) are in a string.
I tried with this:

Code:
$gameVariables.setValue(243, ($gameVariables.value(267).match(/,/g) || []).length)

If I insert in the string(267) a "," or a "!" it seems to work and var(243) returns 1 occurrence.

But if var(267) = "test text?" and I use match(/?/g), then var(243) returns 0.
With other punctuation it returns even stanger numbers...
For example if I try to count how many "." or "..." are in a string it returns respectively 14 and 5 even if I put only 1 of them in the string...
It's clear I'm missing something but I can't understand what :kaocry:
 

caethyril

^_^
Global Mod
Joined
Feb 21, 2018
Messages
2,860
Reaction score
2,196
First Language
EN
Primarily Uses
RMMZ
Regular expressions have a number of special characters, including ? and .. If you want to check for those specific characters then you will need to \escape them. E.g.
  • ? -> "Hello? Is anyone there??".match(/\?/g).length
  • . -> "Who. Cast. That. Slow. Spell. On. Me?!".match(/\./g).length
  • ... -> "What an...uh...interesting idea.".match(/\.{3}/g).length
For more details:
 

Mark91

Veteran
Veteran
Joined
May 19, 2017
Messages
155
Reaction score
18
First Language
Italian
Primarily Uses
RMMV
Regular expressions have a number of special characters, including ? and .. If you want to check for those specific characters then you will need to \escape them. E.g.
  • ? -> "Hello? Is anyone there??".match(/\?/g).length
  • . -> "Who. Cast. That. Slow. Spell. On. Me?!".match(/\./g).length
  • ... -> "What an...uh...interesting idea.".match(/\.{3}/g).length
For more details:
Oh! Clear!
Thank you very much!
 

Latest Threads

Latest Profile Posts

unintentionally made a track that sounds straight out of Yume Nikki
Revisiting one of my older projects in a long time to work some more on the mechanics. I've been working on- and off-again for about a year now on recreating the Force and Mystic systems from Wild ARMs. And I think I've finally made some good breakthroughs on that!

Still, it is not perfect and I may post my thoughts at a later time to see if there are any other suggestions or alternatives I've missed.
actor name string, why you gotta be case sensitive like that?
Doing RPG Maker News for 26th October 2021

My entire world, full of monsters and treasures, will likely be done by the end of the year. After that it's all NPCs and side quests... then done. I'm never making an open world game again xD

Forum statistics

Threads
116,105
Messages
1,095,828
Members
151,496
Latest member
NewGAmeR
Top