RegExp search within part of string

Since the javascript RegExp object does not offer any in-built substring capabilities and javascript does not allow any pointer magic you have no choice but to use substring. However, unless you are expecting gigantic strings I wouldn’t worry too much substring’s performance. Substring is basically a memory copy which is an incredibly optimized operation at the hardware level (think L1-3 caches, cpu extensions that allow copying 128 bits per clock cycle, etc).

Just for my amusement I offer some creative alternatives to substring:

  1. Keep your lastIndex trick, but add `.{m, n}$’ to the end of your regex:

    • let m be str.length - endIndex.
    • and let n be str.length - lastIndex
  2. use a regex engine written in javascript that has in-built substring scanning.

  3. submit an rfc to Ecma International.

Read more here: Source link