Replacing Mutiple Texts in a string

I have an essay that I want to style/replace certain words.

I have a list of words in an array (WORD1, WORD2, WORD3, WORD4,ECT…), I want to be able to find all words in this array that match and replace them with a string.

WORD1 would become |WORD1|

For Example:

" ```

Lorem WORD1 ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut WORD2 aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in WORD3 culpa qui officia deserunt mollit anim id est laborum.

``

New Text

" ```

Lorem |WORD1| ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut |WORD2| aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in |WORD3|culpa qui officia deserunt mollit anim id est laborum.

``

I have tried to use a template column, however the left side you can only input custom text, not a glide column. So I can’t use the list I have in my array. Same with the JAVASCRIPT code option.

For a different but similar function, I was able to do this by manually typing in the equivalent to WORD1, WORD2 as I only had about 10 words. For this list it will become rather large and updated over time. Any Ideas would be helpful?

Hola!

I think you need something like this right?

The trick is to use your Glide column in Template’s parameter field .

Saludos @Ryan599!

2 Likes

Yes, but as I said in my post. The left side of the Replacement section will not allow my to use a Glide column, only custom text. This would not be a problem if I had a small number or words and it what I used in my app elsewhere. However I will have hundreds of words that will constantly changed and added, so this is not a practical solution. I guess effectively I want to be able to use an array as the source of the Replacement words.

I would probably use a javascript column in this case. One parameter would be the text, and the other parameter would be the array (comma delimited list of words). The script would split the comma delimited list into an array, and then loop through the array and do a replace on the text string for each word. Finally return the result.

1 Like

I think this will work at second glance. I was not able to add an array, but using a joined list should work in it’s place. I’ll responed back if it works

1 Like

Ah!, now I understand your issue better.
The option left is what Jeff wrote above: a script that runs a loop finding/replacing your saved words in an array (created by a list) but keep in mind that if your table has many registers (e.g. 2k-5k rows) and your word list to be replaced is large (30+), this process can hit your APP’s performance when it is executed.

Saludos!

1 Like

I’m so close, this code runs in online editors, I’m able to use Console.log() to see that the result is running correctly. However it’s not showing up in the glide colume, I’m not sure what I’m doing wrong. I think it has something to do with how I return the result

  • The StyleBegin and StyleEND var are not showing prporly in the form

JAVA

let Names = p1.split(“|”);
let StyleBegin = “HTML Code”
let StyleEND = “HTML Code”

if (StyleBegin != “g”){
return Names.forEach(string => {
p2 = p2.replace(string, (StyleBegin.concat(string ,StyleEND)));
console.log(p2)
})
}

P1 NAME1|NAME2|NAME3|NAME4

p2 I said hi to NAME1, NAME2 said that NAME3 is very cool. NAME4 doesn’t like being called NAME4.

Don’t use console.log. All you need is ‘Return P2’.

Also, you’ll need to have that outside of your for each loop. Actually outside of the If.

3 Likes

Could you assit me a little? I’m like a level 1 Javascripter.
I have tried Return p2 in a few places and nothing happens or I get an error.

image

javascript is case sensitive. Make sure it’s a lowercase p2.

1 Like

OMG! You are my hero!

1 Like

Glad you got it figured out.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.