JS Regular Expression

I’m not sure if this is the appropriate place for this, but it’s short (and I found it in a JS file):


What does this mean? “\d” is for digits, but “\.” matches all digits and then some, right? Or is “\.” for a literal period? And what is “\-”? Does that mean "look for the literal ‘-’ " or is it a special symbol?

/ - start regex
^ - match start of string
[\-\d\.]+ - match “-”, any digit, and “.”, 1 or more times (the +)
$ - match end of string
/ - end regex

So you can match strings like 1.1219-1.2121-1.2331…1.12331.–

PS. So yes, \. is leteral dot, \- is literal dash (you need the \ since these are reserved characters is regex)

You don’t need to escape the dash if it’s the first character in the character class (the square brackets), otherwise you do (because it indicates a range of characters). And you don’t need to escape the dot in a character class either, only outside it. So you can reduce it to:


Good to know this is possible.
But I think I personally would prefer to escape them anyway, to avoid confusion later on …

Apparently the only characters that do really need escaping inside a character class are \,[B][1],- and ]

Obviously the order of those characters comes into play.

So I’m guessing the fact the - is at the beginning of the class the way it is, means it doesn’t need escaping


  1. /B ↩︎

That’s right. Since the dash indicates a range, if it’s at the beginning it can’t possibly be there to indicate a range, so it’s taken literally.

Sure it can indicate a range there.

[-5] could indicate the range [0-5] with the 0 omitted, only it wasn’t implemented this way. (might be in the future and/or in other languages though!)

That’s a bit of a stretch - I don’t think -5 would ever be interpreted as “0-5”; more like minus five or, as it (sensibly) is at the moment, “dash or five”.

As long are there people that find it interesting to program with only spaces, tabs, and line ends, there will also be people who find it interesting to treat [-5] as [0-5] …

Ah, whitespace! As awesome as LOLCODE and Brainf*ck : )

Javascript uses Perl Compatible Regular Expressions, where the rule is that a - starting (or sometimes, ending) a character class are not considered to be part of a range (ranges are not implied without a start and a stop inside ). However, you’re not going to hurt anything by escaping it, and maybe is safer for anyone going between languages who maybe use other types of regexes.

So far as I know.

Beware though, that JavaScript’s implementation of PCRE is incomplete. For example, it doesn’t support lookbehinds (but it does support lookaheads).

So reverse all involved strings and do a lookahead, which is than a lookbehind :smiley:

It’s amazing how far off-topic this thread is going…

It’s amazing how far off-topic this thread is going…

So? It’s good stuff! Yeah Raf, I saw that on another thread around here and I’m glad I’d run across it!

But lack of advanced features shouldn’t affect basic stuff like [-a-ZA-Z] though right?

It’s not very far off-topic, until you start talking about your favourite sub-species of lemur or what method you use to put your socks on in the morning.

Yeah, the basic stuff (and most of the advanced stuff) is very portable across languages.


until you start talking about your favourite sub-species of lemur

Ringtails aren’t a subspecies but they’re my favourites because of the awesome tails and the way they tip their heads back while eating fruits so as not to spill the juices[/ot]

[ot]Mine is the black lemur, just because of the crazy eyes and the white fluff around the neck.

I didn’t know that about the tipping of the heads. I bet it makes them look like they’re thoroughly enjoying eating.[/ot]