I have a property that can accept different formats as values:
format1 - 2 values separated by space
ex1: left center
ex2: center bottom
format2 - 2 values separated by space, each one can have 1 or 2 values separated by "-"
ex1: left-right center
ex2: center top-bottom
format3 - 2 values separated by "-"
ex1: left-diag
ex2: right-diag
Right now I am splitting the string and using a series of if blocks to figure out what the format and the values.
Is it possible to use regular expressions in this case to figure out the format and to retrieve the values in each format ?
(JavaScript code)
This should do it if you need to use a single regex:
Based on the logic that if there is a space it is the delimiter, otherwise
-is the delimiter. First group matching everything to the delimiter, and second group the rest.Example usage:
On the other hand, you don’t have to use such a complex expression.
Something as simple as this would do: