regexp-make-js icon indicating copy to clipboard operation
regexp-make-js copied to clipboard

Flags in tag

Open zloirock opened this issue 10 years ago • 8 comments

The problem of current conception in flags. Because of them, RegExp.make should return a function. What about simple solution:

RegExp.make`${source}`
RegExp.make('igm')`${source}`
// ->
RegExp.make`/${source}/`
RegExp.make`/${source}/igm`

? Like literals, all tags should begin from a slash, the last slash should show the end of source, slashes in substitutions should be escaped. I think it will make usage this method more readable.

zloirock avatar Oct 03 '15 14:10 zloirock

Actually I really like the feature of the proposed RegExp.make that it would not be necessary to escape slashes. The delimiters of the template string are the ``` (and ${}), and only they should (need to) be escaped. Given that many of my regexes match against a slash, but hardly any of them match against backticks, they are much more readable without this.

bergus avatar Oct 06 '15 20:10 bergus

In PHP one writes

preg_replace("/foo/i", ...)

so there's certainly precedent for having two layers of delimiters and we could look at whether this is a source of confusion or not.

I dislike this though I think the reasons are more aesthetic so the below may be post-hoc.

In

RegExp.make`\\b`

it seems clear that the \\ specifies one literal character to RegExp.make

When there are two levels of delimiters

RegExp.make`/\\b/`

it seems less clear whether RegExp.make pulls out \b or \\b as the body of the regex.

mikesamuel avatar Oct 08 '15 17:10 mikesamuel

In PHP one writes preg_replace("/foo/i", ...) so there's certainly precedent for having two layers of delimiters and we could look at whether this is a source of confusion or not.

I'd say so, yes: http://stackoverflow.com/search?q=[php]+[regex]+escape+literal+delimiter (though of course I cannot say that RegExp.make('igm')…`` is exactly intuitive, yet we'd hopefully get no confusion about delimiters at least).

PHP's approach with the flexible delimiter ("/foo/i" == "~foo~i" == "%foo%i") is at least interesting, and we should consider it as an alternative, but none that I like. Probably for the same aesthetic reasons as you do.

bergus avatar Oct 08 '15 19:10 bergus

PHP allows custom delimiters in every single place a regex is accepted. It wouldn't make sense for JS to add that "feature" in just one place - I think that's likely a non-starter :-)

ljharb avatar Oct 08 '15 19:10 ljharb

JS hasn't another places for regex in tags.

RegExp.make('igm')`${blah}blah${blah}`

looks like something crazy. Anywhere flags should be added after source.

zloirock avatar Oct 08 '15 20:10 zloirock

ljharb, please excuse my ignorance. Are custom delimiters in PHP perl-like?:

$foo =~ m(I want to use '/' in my regex but don't need parentheses);

I see http://www.php.net/manual/en/regexp.reference.delimiters.php but have trouble understanding how it works unless PHP switches into regular expression parsing when it realizes that it's parsing an argument to a function that takes a regex? Is that what you mean by "every single place a regex is accepted?"

If I've understood, I agree that ES shouldn't be deeply context-sensitive.

mikesamuel avatar Oct 09 '15 18:10 mikesamuel

RegExp.make('igm')`${blah}blah${blah}`

looks like something crazy. Anywhere flags should be added after source.

zloirock, I agree that inverting the order is counter-intuitive.

I prefer high-order bits first so, in other languages, I often write /(?i)foo/ to /foo/i.

The (?i) lets the reader know that the whole is case-insensitive before they wade into the body, and /(?s)foo=.*/ lets the reader know what dot means before they encounter them.

So while counter-intuitive, I think it does make for more readable code once you've gotten used to it.

mikesamuel avatar Oct 09 '15 18:10 mikesamuel

@mikesamuel yes - it's been awhile since i've phped but as i recall, like in JS, places that expect a regex will take a string, determine if it's surrounded in matched delimiters, and if so, treat it like a regex.

ljharb avatar Oct 09 '15 19:10 ljharb