RMMV How to count characters occurrences in a string?

Mark91

Veteran
Veteran
Joined
May 19, 2017
Messages
152
Reaction score
17
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,771
Reaction score
2,123
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
152
Reaction score
17
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 Posts

Latest Profile Posts


1,Randomly generate events with planted spawner events(great flexibility for customization and auto-match player's level)
2,Randomly enter roguelike dungeon
3,Player can exit dungeon or back to town or choose to challenge BOSS at the end of each dungeon
4,Dungeon maps can be randomly generated by a third-party tool
Got locked out of the house yesterday... best friends who had the spare ghosted me. Locksmith had to literally drill thru the lock bc they couldn't pick it. Whole process took 4 hours and far too much money. And they have to come back next week bc they don't have the right cylinder, cause it's an old interlocking lock. I'm livid lol
 I’m probably going to regret this, but I need kobolds for my project and I’ve decided to sprite them myself. This is Attempt #1, front view only.

KoboldFront.gif
It's a shame that Selection Control will not be ported to MZ. I found a neat way of using it to enable ways for tanks to protect squishies. lol
Lately I have been taking a break from game and resource making so I won't get burnt out. But I am also interested in the Game Jams this site periodically holds. It'd be great if I knew when the next one, if any, is planned.

Forum statistics

Threads
115,354
Messages
1,089,218
Members
150,029
Latest member
Cwiras
Top