0

Is there a way to use javascript's regex engine to create a character class that matches with

" or " (one space before ") or . or , or [SM_l] or nothing (not the string "nothing" , just 0 characters)

Background Context: This is going to be used as part of the solution to solve the problem presented in this post: Javascript - how to use regex process the following complicated string

Foobar
  • 7,458
  • 16
  • 81
  • 161
  • What did you try? – 31piy Apr 19 '18 at 04:42
  • I have no clue on how to do this. This is part of a way to solve a different, larger, problem that I have already tried solving. I edited my post with context. – Foobar Apr 19 '18 at 04:47
  • Isn't it the same issue as described in https://stackoverflow.com/questions/49912307/javascript-how-to-use-regex-process-the-following-complicated-string? – Wiktor Stribiżew Apr 19 '18 at 06:30
  • @WiktorStribiżew I didn't think it was the same thing - the previous post can have multiple different solutions. For example - I solved the previous problem with regex in a completely different manner, than the way I am describing here. – Foobar Apr 19 '18 at 15:00

1 Answers1

1

You don't a character class. A character class [...] denotes a match on every individual unit of data in it by which["]+ means characters &, q, u, o, t or ; in any order, with or without all characters:

  • "
  • &uot;
  • ;&

What you need is called grouping. You just need a | inside a grouping construct to imply OR conditions (that I also applied in an answer to your original question)

" or " (one space before ")

means [ ]?"

. or , or [SM_l]

means (\.|,|\[SM_l]) that could be reduced to ([.,]|\[SM_l])

Putting all together you need:

([ ]?"|[.,]|\[SM_l])?

Question mark denotes an optional match.

revo
  • 47,783
  • 14
  • 74
  • 117
  • I noticed in your previous answer that you said ` ?"` instead of `[ ]?quot;` is there any real difference between the two, or is one better than the other? – Foobar Apr 19 '18 at 15:27
  • No there is no real difference. Literal spaces are vague inside code blocks. Hence, I used a character class to overcome this issue. – revo Apr 19 '18 at 15:42