How to apply multiple escape characters to the JavaScript

Krzysztof Nowicki 61 Reputation points
2025-02-27T16:25:34.2333333+00:00

What is the best way to apply multiple escape characters to the JavaScript below so they won't collide with each other? Currently, if I apply escape characters to " and \, it invalidates the JSON.

Sample payload:

User's image

Escape chars:

 let choVal = gotVal.replace(/"/g, '\\"');

 let evalue = choVal.replace(/\\/g, '\\\\')

Result Invalid json

User's image

JavaScript code

function replacefunc(jsonString) {

    const match = jsonString.match(/"Extra_Information_on_Label"\s*:\s*"(.*?)"(\s*,\s*"\w+":)/s);

    

    if (match) {

        let gotVal = match[1]; 

        let choVal = gotVal.replace(/"/g, '\\"');

        let evalue = choVal.replace(/\\/g, '\\\\')

        return jsonString.replace(match[0], `"Extra_Information_on_Label": "${evalue}"${match[2]}`);

    }

    throw new Error("Extra_Information_on_Label property not found in the given JSON string.");

}

let data = workflowContext.actions.Set_variable.inputs.value;

if (!data || typeof data !== "string") {

    throw new Error("Set_variable.inputs.value is undefined, incorrect, or not a JSON string.");

}

let out = replacefunc(data);

return out;

Special characters to handle in above JavaScript together

User's image

@RithwikBojja

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

1 answer

Sort by: Most helpful
  1. RithwikBojja 240 Reputation points Microsoft Vendor
    2025-02-27T17:02:56.0833333+00:00

    Hi @Krzysztof Nowicki,

    Below is the design which gives expected results:

    Firstly, I had set the string variable 'var' as below:

    enter image description here

    Then used below actions:

    enter image description here

    Code:

    
    function rithreplcefunc(instr) {
    
        const ri_match = instr.match(/"Extra_Information_on_Label"\s*:\s*"(.*?)"(\s*,\s*"\w+":)/s);
    
        if (ri_match) {
    
            let rigoVal = ri_match[1]; 
    
            let ri_eg_val = JSON.stringify(rigoVal).slice(1, -1);
    
            return instr.replace(ri_match[0], `"Extra_Information_on_Label": "${ri_eg_val}"${ri_match[2]}`);
    
        }
    
        throw new Error("Hello Rithwik, Extra_Information_on_Label property not found in JSON string.");
    
    }
    
    let ri_dta = workflowContext.actions.Set_variable.inputs.value;
    
    if (!ri_dta || typeof ri_dta !== "string") {
    
        throw new Error("Set_variable.inputs.value is undefined, incorrect, or not a JSON string.");
    
    }
    
    let ri_res = rithreplcefunc(ri_dta);
    
    return ri_res;
    
    

    Output:

    enter image description here

    Hope this helps.

    If the answer is helpful, please click Accept Answer and kindly upvote it. If you have any further questions about this answer, please click Comment.


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.