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

4
  • What did you try? Commented Apr 19, 2018 at 4: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. Commented Apr 19, 2018 at 4:47
  • Isn't it the same issue as described in stackoverflow.com/questions/49912307/…? Commented Apr 19, 2018 at 6: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. Commented Apr 19, 2018 at 15:00

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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?
No there is no real difference. Literal spaces are vague inside code blocks. Hence, I used a character class to overcome this issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.