Using replace to change different strings?

For example, in the sentence, "All cats are mammals", I'd like to use replace to change it into "All lizards are reptiles"

I know how to change one string:

{string="All cats are mammals"}
{=replace(string, "cats", "lizards")}

But I don't know how to change multiple strings.

You can nest the replace() calls:

{string="All cats are mammals"}
{=replace(replace(string, "cats", "lizards"), "mammals", "reptiles")}

Got it. Thanks :slight_smile:

Any possibility this might be simplified in the future?

This is unlikely to be changed. Optimizing this specific use case probably wouldn't be worth the increase in the complexity of the function.

Ok, no worries :grinning:

I've created a "ready made" snippet for people who want to do it the easy way with formtext

This is the text you want to modify:
"{formtext: name=string; default=All cats are mammals}"

Replace "{formtext: name=string1old; default=cats}" with "{formtext: name=string1new; default=lizards}"

AND

Replace "{formtext: name=string2old; default=mammals}" with "{formtext: name=string2new; default=reptiles}"

This is the modified text:
"{=replace(replace(string, string1old, string1new), string2old, string2new)}"

2 Likes

@Cedric_Debono_Blaze Can regex be used to delete text from a field? Example: I have a Salesforce case with text fields open for editing. There is text in the subject field that I want to have deleted (or replaced with . I can simulate keypresses to repeat the backspace key 26 times but that is slow & cumbersome. I am looking for a solution that when I run a snippet, it looks for a string of text and deletes it. Possible?

You can delete parts of text with Regexs, but you can't use them to directly manipulate the page.

So if you had a text on the page with some text you would need to delete it with key commands.

Rather than deleting in character by character, a faster approach might be to select all ({key: ctrl-a} or command-a on a mac) and do a single backspace to delete it all at once, and then insert your replacement text.

Gotcha. Yeah I use that method wherever I can. My use case here is interesting I think. I have snippets that create new cases in Salesforce. There are critically important data points that are not presented on the screen though until after the case is created, because the data pulls from the SF Contact tables. Therefore, I have a 2-snippet approach.

Snippet 1:

  1. Creates the case.
  2. When case is displayed, there is a wait command followed by a series of simulated key presses that are Salesforce shortcuts to get the new case opened in edit mode.
  3. The cursor is automatically placed at the end of the Subject field that is now an "open for edit" text field.
  4. The last step of this first snippet is to put a string of text and the cursor in the Subject field that is only there as an instruction to the user to now trigger a 2nd snippet. It says "TYPE *9 ON YOUR KEYBOARD NOW!"

Snippet 2:

  1. The user presses *9 as instructed to run the 2nd snippet.
  2. This starts a {repeat: 29}{key: backspace}{endrepeat} that deletes the instructional message above, 1 character at a time.
  3. Then tabs down to the case Description, adds a couple blank lines at the top, and pulls in needed data points via regex because those data points are now visible on the screen where they were not when the case was initially created.

Here is a video walk through of the above. Thoughts on how to better approach this?
https://drive.google.com/file/d/1f0D-dyAWmRo2WCTVKSfKL2wXsXCUbpyb/view?usp=share_link

For step 2.2, can you do {key: ctrl-a}{key:backspace} (assuming you are on Windows, adjust if you are on Mac) do delete all the text in 2 key strokes instead of 29?

No - because I do not want to delete the entire subject contents. Just want to delete the note telling the user to run the 2nd snippet. And, it is that 2nd snippet that pulls in the needed info via regex.

OK, then how about something like this:

{key: ctrl-a}{key:backspace}{=left({site: text; selector=YOUR FIELD SELECTOR HERE}, len({site: text; selector=YOUR FIELD SELECTOR HERE}) - 29)}

This should delete the field contents and the reinsert it minus the last 29 characters.

That actually works fairly well! Only issue I am encountering in testing is if I have another case tab open in edit mode. This snippet replaces the Subject contents with the contents from the other open case. Hmmm...

You can probably get fancy using the :focus or :focus-within CSS selector to get the right element when you have multiple tabs.

@scott Hey! Figured out a way to do this with regex!

{key: ctrl-a}{=left({=extractregex({site: text}, "[\s\S]\nSubject\n(.+)")}, len({=extractregex({site: text}, "[\s\S]\nSubject\n(.+)")}) - 29)}

1 Like