\$ in regexps

# Michael Day (15 years ago)

[sent this to es3.x-discuss by mistake, sorry for any duplicates]

Hi,

Is $ valid inside regexps? For example:

xs = /foo$bar/.exec("foo$bar");

This matches in Firefox. However, the spec would appear to forbid this interpretation, unless I'm confused again:

IdentifierStart :: $

IdentifierPart :: IdentifierStart

IdentityEscape :: SourceCharacter but not IdentifierPart

CharacterEscape :: IdentityEscape

AtomEscape :: CharacterEscape

Atom :: \ AtomEscape

From these rules it seems that $ is forbidden, and the only way to match a $ is to use a HexEscapeSequence or UnicodeEscapeSequence.

So is this a spec bug? It seems to be present in ES3 and ES5.

Or a Firefox extension?

Best ,

Michael

# Brendan Eich (15 years ago)

On Oct 30, 2010, at 4:49 AM, Michael Day wrote:

/foo$bar/.exec("foo$bar")

This works in all browsers I can test atm (Chrome, Firefox, Opera, Safari).

It's an old de-facto standard, going back to the dawn of RegExp in Netscape 4-era SpiderMonkey (pre-ES3), and based on lots of Unix precedent: \ followed by any non-special char means literal next.

It is a bit future-hostile, or was 12 years ago -- we can't add more escapes easily. But that decision can't be recalled and changed, and anyway, a fixed set of escapes may be best on the Web.

ES3 and ES5 didn't codify it. Harmony should. Thanks for pointing it out.