Hacker News new | past | comments | ask | show | jobs | submit login

I guess unpopular opinion: I think this library is excellent especially for making readable regex match/replace code. Even as a frontend dev with 5+ years of experience, it's difficult to read regex by skimming it (perhaps that's just b/c I don't spend much time with it at all outside of matching user inputs). Hopefully, if this library catches on, it may make JS code using regex slightly more readable. Regardless, props to the author for making this library.



Let me save you 8 kilobytes of dependency and a new API to learn (and debug, when you start wondering why your ReX built regex isn't working how you want). Apparently the main "feature" of ReX is you get to annotate the components of a regex string.

    // match something that looks like a date in the form 02-14-1924
    let x = "";
    x += '\d{2}'; // match the month 
    x += '\-';
    x += '\d{2}'; // match the day
    x += '\-';
    x += '\d{4}'; // match the year
Even this is horribly overcomplicated example would just be `\d{2}\-\d{2}\-\d{4}`. The Rex library provides no useful abstractions to simplify vanilla javascript and regex.

If a (dynamically built) regex misbehaves, I'm only inspecting the lines of code that compose the string, not an entire library to understand what went wrong.


No need to escape the dashes btw:

    $ node
    > '02-14-1924'.match(/\d{2}-\d{2}-\d{4}/)
    [ '02-14-1924', index: 0, input: '02-14-1924', groups: undefined ]


That's poor usage of regex. Try this:

    $ node
    > let XRegExp = require('xregexp');
    > XRegExp.exec('02-14-1924', XRegExp(`
        (?<day>  \\d{2} ) -
        (?<mon>  \\d{2} ) -
        (?<year> \\d{4} )
    `, 'x'))
    [ '02-14-19',
      '02',
      '14',
      '1924',
      index: 0,
      input: '02-14-1924',
      groups: undefined,
      day: '02',
      mon: '14',
      year: '1924' ]


You may want to check out my lib, compose-regexp, which serves a similar purpose with a smaller API and weight (< 1KB mingzipped).

Named captures can be created with the following helper (with an example that parses strings):

    import {avoid, capture, either, sequence, suffix} from 'compose-regexp'

    function makeNamedCaptures(indices) {
      let i = 1
      return function namedCapture(name, exp) {
        if (indices.hasOwnProperty(name)) {
          throw new Error("\"" + name + "\" is already used as a capture name")
        }
        indices[name] = i ++
        return capture(exp)
      }
    }

    const captureIndices = {}, namedCapture = makeNamedCaptures(captureIndices)

    const zeroPlus = suffix('*')
    const any = /[\s\S]/
    const anyNonReturn = /./

    const stringMatcher = sequence(
      namedCapture("quote", /['"]/),
      zeroPlus(
        avoid(ref(captureIndices.quote)),
        either(
          sequence('\\', any),
          anyNonReturn
        )
      ),
      ref(captureIndices.quote)
    )


    // stringMatcher is actually a plain regexp:
    console.log(`"foo'\\"'bar"baz`.match(stringMatcher))
    // --> [ '"foo\'\\"\'bar"', '"', index: 0, input: '"foo\'\\"\'bar"baz' ]
You can off course parse strings without using back references, the goal here is to provide an example with a simple grammar that you can follow along.

For complex grammar with sub-parts, you can wrap the sb-patterns in functions and inject the `namedCapture` as a parameter.

https://flems.io/#0=N4IgZglgNgpgziAXAbVAOwIYFsZJAOgAsAXLKEAG...




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: