Remove special characters from logic app string

MS Techie 2,751 Reputation points
2024-02-14T18:32:43.6733333+00:00

I am aware that we can use replace() function to remove individual special characters and replace them with ''.But I want to remove all special characters from the logic app string variable and allow only content which is a-zA-Z and 0-9. Can you please give the approach

Azure Logic Apps
Azure Logic Apps
An Azure service that automates the access and use of data across clouds without writing code.
3,542 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Luis Arias 8,621 Reputation points Volunteer Moderator
    2024-02-19T12:49:27.17+00:00

    Hi MS Techie,

    Unfortunately, Azure Logic Apps built-in functions have the below considerations on your use case:

    • Logic Apps does not support regular expressions.
    • JavaScript inline code action to remove special characters require an automation account and It doesn't support get variable as arguments.

    So you have alternatives solution to remove this characthers:

    • Create an Azure Function in a supported language (like JavaScript or C#) that takes a string as input, removes special characters using a regular expression, and returns the cleaned string.
    • Create an HTTP Trigger eather to your Azure Function or another APi service that it can be called via an HTTP request to remove the special characters
    • Last workaround with Javascript inline code action and workflow context but require an automation account

    Remove special character with javascript inline code action:

    Asociate the Integration account to logic apps on workflow settiong (This automation account must to be in the same azure region)

    User's image

    This is an example workflow removing the special characters :

    {
        "definition": {
            "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
            "actions": {
                "Execute_JavaScript_Code": {
                    "inputs": {
                        "code": "var reg = /[^a-zA-Z0-9]/g;\r\nvar nospecial = workflowContext.actions.variable.inputs.variables[0].value;\r\nreturn nospecial.replace(reg, '');"
                    },
                    "runAfter": {
                        "variable": [
                            "Succeeded"
                        ]
                    },
                    "type": "JavaScriptCode"
                },
                "variable": {
                    "inputs": {
                        "variables": [
                            {
                                "name": "specialVariable",
                                "type": "string",
                                "value": "NoSpecial$$%%^&&>-?+yes"
                            }
                        ]
                    },
                    "runAfter": {},
                    "type": "InitializeVariable"
                }
            },
            "contentVersion": "1.0.0.0",
            "outputs": {},
            "parameters": {},
            "triggers": {
                "Recurrence": {
                    "evaluatedRecurrence": {
                        "frequency": "Month",
                        "interval": 3
                    },
                    "recurrence": {
                        "frequency": "Month",
                        "interval": 3
                    },
                    "type": "Recurrence"
                }
            }
        },
        "parameters": {}
    }
    
    

    The execution will be successfull:

    User's image

    I hope this information help you.

    Luis


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.