Hi guys, i having a confusion about how string.match() function work.
I saw inside of Yanfly item core, he check for a <TEXT COLOR: x> note match like this:
line.match(/<(?:TEXT COLOR):[ ](\d+)>/i)
but why ?:
it should work without ?: but why did he include it?
if it for checking zero or one of : shouldn't he wrote :? instead?
String.match confusion?
● ARCHIVED · READ-ONLY
-
-
? means 0 or 1. And : is an empty symbol. Yanfly uses it to allow people to type it in any way among:
<text color: 5>, < text color: 5>, <text color:5> or < text color:5>. -
Thanks @Poryg i didn't know that : is an empty symbol : E
-
It's not mentioned in too many places, took me a bit to find it myself :D
-
Wow that true. I had read on w3school & searched google but it didn't mention about this.
That's weird : | oh right, there is something more that trouble me that i want to ask @Poryg
Why match(/<C/i) didn't work?
I mean i have this str = <CT>
i tried str.match(/</i) and str.match(/CT/i) - both worked
but when i tried /<C/i or /<CT>/i it returned nothing. : |
do you know why? -
Perhaps < is a code as well? Try <:C if you want. I have seriously no idea though :D
-
<:C didn't return anything too though. : | maybe you're right. No one mentioned about this :|
-
Tried both in Ruby and JS, both works though.
-
in that case maybe she has it wrong somehow :D
-
@TheoAllen i tried to run the code on this side, but the result won't show up
https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_match_regexp
perhaps the site has problem? -
@Isabella Ava you tested it in a browser which read HTML tag. So, of course, an incomplete tag like '<' with missing '>' as end can't show up. Even if it's complete <CT> you couldn't even show it in the browser because the tag is unknown.
I usually use https://repl.it/ to test stuff -
No, it doesn't have a problem. <> are html tags, while just < is not.
Edit: Ninja'd :D
try alert(res). You'll see it shows. -
Thanks @TheoAllen and @Poryg
Well that's totally unexpected : ) (turn out that Editor read HTML tag sigh) -
The ?: means the group inside bracket ( ) won't be captured, so the first element in returned match in your example (ignore the match[0] is the whole string) will be the number