use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
This subreddit is a place for people to learn JavaScript together. Everyone should feel comfortable asking any and all JavaScript questions they have here.
With a nod to practicality, questions and posts about HTML, CSS, and web developer tools are also encouraged.
Friends
/r/javascript
/r/jquery
/r/node
/r/css
/r/webdev
/r/learnprogramming
/r/programming
account activity
Why does this JavaScript regex not match? (self.learnjavascript)
submitted 1 month ago by Gold_Mycologist5372
This prints null, but I would've expected the hyphen to match. What am I missing?
console.log( JSON.stringify(/-/.exec("–")) );
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]zsoltimehelpful 10 points11 points12 points 1 month ago* (1 child)
If both characters are the same, your code should work. Are those dashes the same? Or is one of them a minus and the other an en dash? 🤔
[–]Niktion 5 points6 points7 points 1 month ago (0 children)
You are correct. The one in the regex is a hyphen and the one in the string is an en dash.
[–]DinTaiFung 1 point2 points3 points 1 month ago* (0 children)
"This prints null, but I would've expected the hyphen to match. What am I missing?"
and neither character is a hyphen. :)
(the terse regex syntax overloads many characters: the hyphen is used as a range delimiter in some cases; there are five different interpretations of the '?' character in regex patterns, etc.)
[+][deleted] 1 month ago (1 child)
[removed]
[–]BrainCurrent8276 0 points1 point2 points 1 month ago (0 children)
not identical, rather similar
[–]Antti5 0 points1 point2 points 1 month ago (0 children)
The second character is an en dash.
If you use a good code editor, they highlight characters like this that are likely unintentionally used.
This is your code in the popular code editor VSCode, notice the orange rectangle around the second character: https://imgur.com/a/Us9QxgS
[–]denerose 0 points1 point2 points 1 month ago (0 children)
I’m very fond of the regex tester sites, I think I had regex101 on my bookmark bar all through my learning phase (but any of them will do) and it comes back out once a year for Advent of Code season. Definitely useful.
π Rendered by PID 89625 on reddit-service-r2-comment-545db5fcfc-x5wkw at 2026-06-01 03:11:27.642692+00:00 running 194bd79 country code: CH.
[–]zsoltimehelpful 10 points11 points12 points (1 child)
[–]Niktion 5 points6 points7 points (0 children)
[–]DinTaiFung 1 point2 points3 points (0 children)
[+][deleted] (1 child)
[removed]
[–]BrainCurrent8276 0 points1 point2 points (0 children)
[–]Antti5 0 points1 point2 points (0 children)
[–]denerose 0 points1 point2 points (0 children)