Replace Values from Array in a string and provide the updated string

Hi I have an long string and an array that has 4 values . I want to replace all those 4 values from Array with a new value in long string and at the end of execution I want a new variable with all updated values. Can you guide me how to accomplish this.

Welcome to the Make community!

When reaching out for assistance with your regex pattern, it would be super helpful if you could share the actual text you’re trying to match. Screenshots of text can be a bit tricky, so if you could copy and paste the text directly here, that would be awesome! It ensures we can run it against test patterns effectively. If there’s any sensitive info, feel free to change it to something fictional yet still valid by keeping the format intact.

Providing clear text examples saves time on both ends and helps us give you the best possible solution. Without proper examples, we might end up playing a guessing game, and nobody wants that as it is a waste of time! You are more likely to get a correct answer faster. So, help us help you by sharing those text snippets. Thanks a bunch!

samliewrequest private consultation

Join the Make Fans Discord server to chat with other makers!

1 Like

Hi

Thank you for your reply. I will try to provide more context . I want to recreate same thing using make.com. Can you provide an outline of how to replicate this in make.com I am just looking for some idea.

In this function:text is the variable containing your long text.
oldLinks is an array of old links you want to replace.
newLinks is an array of new links to replace the old links with.
The function iterates over the oldLinks array, creates a regular expression for each old link (to replace all instances globally), and replaces it with the corresponding new link from the newLinks array.

function replaceLinks(text, oldLinks, newLinks) {
    if (oldLinks.length !== newLinks.length) {
        throw new Error("Old links and new links arrays must be of the same length.");
    }

    for (let i = 0; i < oldLinks.length; i++) {
        const oldLink = oldLinks[i];
        const newLink = newLinks[i];
        const regex = new RegExp(oldLink, 'g');
        text = text.replace(regex, newLink);
    }

    return text;
}

// Example usage:
const text = "Here are some links: http://oldlink1.com and http://oldlink2.com.";
const oldLinks = ["http://oldlink1.com", "http://oldlink2.com"];
const newLinks = ["http://newlink1.com", "http://newlink2.com"];

const updatedText = replaceLinks(text, oldLinks, newLinks);
console.log(updatedText); // Output: Here are some links: http://newlink1.com and http://newlink2.com.

see 🔎 Variable Replace All: Replace a string array with another string array

samliewrequest private consultation

Join the Make Fans Discord server to chat with other makers!

1 Like