Search code examples
javascriptregexcharacter-class

Javascript - Create Regex Character Class


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


Solution

  • 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.