Skip to content

Multiline Regexp by also ignoring linebreaks #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
martinheidegger opened this issue Oct 19, 2022 · 6 comments
Open

Multiline Regexp by also ignoring linebreaks #5

martinheidegger opened this issue Oct 19, 2022 · 6 comments

Comments

@martinheidegger
Copy link

It seems like a low hanging fruit to also ignore linebreaks as characters that are ignored in x mode that would open the door to good multi-line RegExp definitions.

r = /
  [0-9]{1,4} # the first few digits
/x

It maybe good to have an FAQ explainer that explains this absence (or support it)

@rbuckton
Copy link
Collaborator

That's probably not feasible. Implementations are adamant that they will not change how https://tc39.es/ecma262/#prod-RegularExpressionLiteral parses. In addition, that would be a breaking syntactic change due to ASI:

a
/
b
/x

Is legal JS, and a multiline RegExp would make this ambiguous or require a fairly complex cover grammar.

Some of these limitations are also being discussed in #1 as it pertains to what is allowed inside of a (?#) comment.

@martinheidegger
Copy link
Author

martinheidegger commented Oct 19, 2022

This is a very fascinating reply 🤩 that I think in itself would definitely enrich the readme.

That said I find this proposal a lot less appealing without dedicated support for Multiline regexp Grammar (preferably not conflicting with existing grammar 😅) as I would like IDE support (code highlighting, linting and inline docs) for Multiline regexp. If I have to put it in a string something like this is syntax highlighted.

x = new RegExp(
  '[0-9]{1,4}' // the first few digits
)

Maybe an alternative Syntax like:

x = RegExp/
  [0-9]{1,4} # the first few digits
/m

Could work out?

@rbuckton
Copy link
Collaborator

Unfortunately, the RegExp/ syntax also wouldn't work because a valid regexp like /a/ would again be parsed as division: RegExp/a/x.

The most likely scenario is using a template literal. The examples I've made so far use this:

x = new RegExp(String.raw`
  [0-9]{1,4} # the first few digits
`, "x");

The template isn't so bad because ${ is not legal RegExp syntax, though you still have to escape `.

@martinheidegger
Copy link
Author

/a/ would again be parsed as division

You lost me there, but maybe my suggestion was poor. I was trying to suggest that [RegExp/] be treated as a keyword. Thinking about it a tad more that would be quite hacky.

The template isn't so bad because ${ is not legal RegExp syntax, though you still have to escape `.

My concern is not about the escaping of content, thought that is annoying, its more about the absence of reliable syntax highlighting.

Rethinking this once more: would it be possible to have Regexp have a multiline helper that Syntax highlighter could treat differently?

x = RegExp.multiline`
  [0-9]{1,4} # the first few digits
`

As I recently noticed that Python IDE's do format similar r templates.

r'[0-9]{1,4}'

@saturdaywalkers
Copy link

Perl does

qx"
 [0-9] {1-4} #2  
"x;

qx/ 
 [0-9] 
 {1-4} # the char after the qx is the delimiter
 /x

This proposal has been stalled for a while.

It would really help as regex easily become line noise without spacing. Having linebreaks is pretty important!

Any movement?

@tophf
Copy link

tophf commented Mar 4, 2023

Multi-line mode is a must. Currently implemented via transform-modern-regexp plugin that uses re tagged literals:

re`/
 
  # A regular expression for date.
 
  (?<year>\d{4})-    # year part of a date
  (?<month>\d{2})-   # month part of a date
  (?<day>\d{2})      # day part of a date
 
/x`;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants