I am writing a regular expression that will target everything to the right of ":" and to the left of a ","
I am using this RegExp:
:(.*?)(,|})
This works well for the most part, except when the expression runs into objects that are on the right side of the ":"
For example, on a list of prop type definitions in TypeScript:
open: boolean,
onDidDismiss: () => void,
message: string,
showAddReceiptModal: boolean,
showModal: (value: (((prevState: boolean) => boolean) | boolean)) => void,
vehicleTabRef: React.MutableRefObject<null>,
onReceiptSubmit: (data) => void,
vehicle: any,
showModal1: boolean,
showModal2: (value: (((prevState: boolean) => boolean) | boolean)) => void,
selectedReceipt: { __typename: "Receipt" | undefined; id: string | undefined; purchaseDate: string | undefined; sellerName: string | undefined; sellerAddress: string | undefined; sellerCity: string | undefined; sellerState: string | undefined; sellerZipCode: string | undefined; totalAmount: number | undefined; gallonsPurchased: number | undefined; image?: string | null | undefined | undefined; userID?: string | null | undefined | undefined; vehicleID: string | undefined; vehicle?: Vehicle | null | undefined | undefined; taxRefund?: number | null | undefined | undefined; createdAt: string | undefined; updatedAt: string | undefined },
onClick: () => void,
onClick1: () => void,
dataEditing: boolean,
toastMessage: (val) => void,
editing: (bool) => void,
showToast: (bool) => void,
showModal3: boolean,
showModal4: (bool) => void,
dateFilter: { startDate: string; endDate: string },
When I run replaceAll with this regex, and the replacement is a comma I am left with this:
open,
onDidDismiss,
message,
showAddReceiptModal,
showModal,
vehicleTabRef,
onReceiptSubmit,
vehicle,
showModal1,
showModal2,
selectedReceipt,
,
onClick,
onClick1,
dataEditing,
toastMessage,
editing,
showToast,
showModal3,
showModal4,
dateFilter,
,
Notice the empty commas, these should not be here. This expression should target everything to the right of the ":" and replace it all with a comma.
What regex can I use to only remove everything to the right of the :
to the ending comma, and just leave that comma?