Cleaning the {{text:Field}} before launching a link

Hi.

One of my fields has words which may or may not have parentheses. Since I want to launch a search of the word on a website with a link when clicking the word or a button, I would like to use the {{text:Field}} functionality to add the word to the URL. However, if the word has () within it, the search will fail.

Is there a way to eliminate these before adding them to the URL?

I’ve thought of creating a new field with the word without the parentheses (just batch copying the word and replacing the parentheses with noting) and using that field for the search instead, which would serve as a workaround. But this deck has already way too many fields, so if there is an alternative I’d like to try it.

Thanks!

Could you make a few examples of some of these words that have parentheses, and how you want them to be before adding them to the URL?

Sure, sorry, I should’ve been more specific.

These are mostly verbs in Spanish, so the parentheses show possible variations. One example would be acordar(se).

Maybe try something like this:

<div id="link_output"></div>

<script>
   var unparsed_link = "<a href='https://dictionary.cambridge.org/dictionary/spanish-english/{{text:Field}}'>{{text:Field}}</a>";

  if (unparsed_link) {
   var parsed_link = unparsed_link.replace(/\((.*?)\)/g, '$1');
   document.getElementById("link_output").innerHTML = parsed_link; 
}
</script>

The code replaces acordar(se) with acordarse.
If you want to replace it with acordar instead, try deleting $1 from var parsed_link = unparsed_link.replace(/\((.*?)\)/g, '$1');.

1 Like

Thanks works perfectly, thank you so much!

1 Like

Hi again! Sorry to bother you. I am trying to use an image as a link in the script you wrote.

I am replacing the var unparsed_link line with

   var unparsed_link = "<a href='https://dictionary.cambridge.org/dictionary/spanish-english/{{text:Field}}'><img src="_book-open-text.svg" style="height: 25px;" title="Definition"></a>";

However, the image is not showing up and the link is lost. Is there a way to fix this?

Thanks in advance!

Actually, even using {{Field}} instead of {{text:Field}} shows acordarse instead of acordar(se), which is the original field.

Besides using an image, Is it possible to show acordar(se) but direct the link to acordarse?

Hi!

  1. Regarding the image issue:
    the problem appears to be that you are using double quotes to wrap the attributes src, style and title, but they were already used for the variable.
    If you use double quotes for the variable, you need to use single quotes for the attributes, and vice versa.

E.g., this version of the code seems to work correctly on my end:

var unparsed_link = "<a href='https://dictionary.cambridge.org/dictionary/spanish-english/{{text:Field}}'><img src='_book-open-text.svg' style='height: 25px;' title='Definition"></a>';

Or alternatively:

var unparsed_link = '<a href="https://dictionary.cambridge.org/dictionary/spanish-english/{{text:Field}}"><img src="_book-open-text.svg" style="height: 25px;" title="Definition"></a>';

  1. Regarding “{{Field}} vs {{text:Field}}”:
    {{text:Field}} is useful when the field contains HTML tags (e.g. <br>, <u></u>, <b></b> etc.), so that they are not included in the link. If the field doesn’t contain any HTML tags, it shouldn’t make much of a difference if you use one or the other. That being said, using {{text:Field}} should almost always be the best practice.

  2. Regarding “show acordar(se) but redirect to acordarse”:
    removing the g (global) modifier ensures that the regex will only remove the first pair of parentheses it encounters (i.e., the ones in the href attribute of the link, not affecting those of the displayed text).

To remove the g (global) modifier, change the regex from
unparsed_link.replace(/\((.*?)\)/g, '$1')
to
unparsed_link.replace(/\((.*?)\)/, '$1')

If sometimes the field contains more than one pair of parentheses (e.g., a(cor)dar(se) ), it should still be possible to adjust the script, but more complex changes would be required.

2 Likes

Oh my god, you are my hero! Works beautifully!

:heart: Thank you so much!! :heart:

1 Like

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