Two different RegEx replacements in one handlebar?
Hello,
Our Amazon Integration App needs to look up the item SKU in Amazon before importing the order. We have different naming conventions in this Amazon, so I need to use RegEx on the returned result to translate the name to our actual SKU. In Amazon export, we use a space in the SKU name, and SKU is appended with "-FBA". I need to replace the first space with a dash (-), and remove the "-FBA" from the end.
For example, I need to translate a name like this:
12223 4556-FBA
To this:
12223-4556
I've been trying to use handlebars to complete this, but I'm not sure if it's possible to replace multiple different matches in the same handlebar. These two formulas work on their own, but I would need to combine them:
{{regexReplace SellerSKU "-" " "}} //returns 12223-4556-FBA
{{regexReplace SellerSKU "" "-FBA"}} //returns 12223 4556
From the looks of the Handlebar Helper reference, it looks like I can only select one condition to replace at a time.
Is there any way that I could nest or combine these two regex matches into a single expression? Any help would be appreciated.
Thanks,
Jake
Comments
I found a workaround to this problem - I was able to use regex to grab the first half of the SKU before the space, regex to grab the second half between the space and "-FBA", and join them together with "-" to get the value I wanted. Here is the handlebar:
{{join "-" (regexMatch SellerSKU "\S*") (regexMatch SellerSKU "(?<=\s)[^-]*")}}
With this handlebar, SKU 12223 4556-FBA would properly output to 12223-4556
Note that the above also did not work in the flow (resulted in invalid quantifier "?"), but it worked in the dev playground .
For anyone else wondering, the reason that this error occurs is because Javascript does not support lookahead / lookbehind, and "?" is involved in the process. You will need to find a different way to structure your formula that avoids lookaheads/lookbehinds.
Here is how I reconstructed this formula, and this now works in the flow:
{{join "-" (regexMatch OrderItem[*].SellerSKU "\S*") (trim (substring OrderItem[*].SellerSKU (regexSearch OrderItem[*].SellerSKU " ") (regexSearch OrderItem[*].SellerSKU "-FBA")))}}
This other community post/link helped me a lot here, I recommend checking this out if you run into the same issue: https://docs.celigo.com/hc/en-us/community/posts/360078177872-Regex-in-handlebar-to-get-everything-after-the-first-whitespace-doesn-t-work-during-flow-execution
Hi Jake Naydock!
Thank you so much for sharing your solution and for letting us know that a previous community post helped you!
Hi Courtney Jordan,
No problem! Do you know why certain things will work in the Dev playground, but not in the flow itself? I was surprised to see that my regex worked when testing, but ended up having to rethink the whole process after trying to put it in place. Sometimes I will notice inconsistencies between Dev playground behavior and flow behavior, and wanted to see if you or anyone has insight as to what causes that.
Thanks,
Jake
Hi Jake Naydock,
Thanks for letting us know about this discrepancy. I am following up with the PM in charge of flow builder.
Please sign in to leave a comment.