Posted over 2 years ago. Visible to the public. Repeats.

Using regular expressions in JavaScript

Regular expressions in Javascript are represented by a RegExp object. There also is a regex literal as in many other languages: /regex/. However, they are used slightly differently.

Regex literal

  • Usage: /foo+/
  • Shorthand for creating a regular expression object

RegExp() object

  • Usage: Regexp("foo+") or new RegExp("foo+")
  • No surrounding slashes required (they're the literal markers)
  • Since the argument is a string, backslashes need to be escaped as well: RegExp("\\d+")


  • Regex objects never equal each other
  • The argument to /regex/.test(...) is converted to a string as defined by the specs, which means e.g. .test(null) is equal to .test("null").
  • Globally matching regex objects remember the last index they matched. Multiple calls to test() will advance this pointer:

    > matcher = new RegExp("foo", "g") // <- "global" flag > matcher.test("foobar") // => true > matcher.lastIndex // => 3 > matcher.test("foobar") // => false > matcher.lastIndex // => 0

    This does not happen when creating a new regex object each run, as with /foo/g.test("foobar").

Does your version of Ruby on Rails still receive security updates?
Rails LTS provides security patches for old versions of Ruby on Rails (3.2 and 2.3).

Owner of this card:

Dominik Schöler
Last edit:
over 2 years ago
by Henning Koch
About this deck:
We are makandra and do test-driven, agile Ruby on Rails software development.
License for source code
Posted by Dominik Schöler to makandra dev
This website uses cookies to improve usability and analyze traffic.
Accept or learn more