I have a regular expression that will target everything to the right of ":" in a map and to the left of a ","
I am using this RegExp:
:(.*?)(,|})
Like this:
$PROP_TYPES.replaceAll(":(.*?)(,|})", ",").concat("}")
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.
This code is written in VTL to use with the Webstorm IDE to create quick typescript templates. But Java string manipulation works as well
What regex can I use to only remove everything to the right of the :
to the ending comma, and just leave that comma?
You might use:
^\s*\"([^\"]+)\":.*(?=,)
Explanation
^
Start of string \s*
Match optional whitespace chars (Or use \h*
in Java to not match newlines) \"([^\"]+)\"
Capture in group 1 all between double quotes :.*
Match :
and then the rest of the line (?=,)
Positive lookahead, assert a comma to the right In the replacement use capture group 1.
The result after the replacement:
open,
onDidDismiss,
message,
showAddReceiptModal,
showModal,
vehicleTabRef,
onReceiptSubmit,
vehicle,
showModal1,
showModal2,
selectedReceipt,
onClick,
onClick1,
dataEditing,
toastMessage,
editing,
showToast,
showModal3,
showModal4,
dateFilter,
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.