Skip to content

What to do when regexp in switch exceeds (desired) length of line? #831

Answered by edemaine
gwhitney asked this question in Q&A
Discussion options

You must be logged in to vote

You could use ///, which seems to work already:

switch x
   /ordinary.*/
      console.log 'Things look normal'
   ///many|different|unusual|possibilities|listed|
      here|because|all|are|handled|similarly///
      console.log 'Yellow alert!'

/// is what I generally gravitate to when I have complex regexes, but note that it eats whitespace, which is nice for formatting regexes (in particular line breaks and indentation are ignored), but it might mean you need to modify your regex a bit.

Riffing on your last paragraph, another feature that might help you would be listing patterns over multiple lines via a trailing comma, like this:

switch x
   /ordinary.*/
      console.log 'Things look …

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by gwhitney
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #826 on December 05, 2023 03:59.