Tinderbox Forum

Capitalization in Text Replace

Consider this action code in a stamp.

Is there a way to simplify this so that one Replace works with both the capitalized and non-capitalized versions of the word?

image

Could the solution generalize if I replace the word “carpal” with a ^value reference to a User Attribute that I define?

As String.replace supports regex what not use such an approach?

So, your stamp becomes:

$Text = $Text.replace("([C|c])arpal",'<font colour="blue">'+$1+'arpal</font>');

But <font> tags went away in the last century (I remember first using them pre-CSS). A style ` would be more current HTML usage. Thus the above becomes:

$Text = $Text.replace("([C|c])arpal",'<span style="color: blue;">'+$1+'arpal</span>');

The latter works in preview, even in a TBX with no export template. Better, IMO would be to add an HTML template that defines, in the head, a style block with CSS for different colours, etc., each as a CSS class. Now, with the blue text colour defined in CSS class ‘term_blue’, your stamp becomes:

$Text = $Text.replace("([C|c])arpal",'<span class="term_blue">'+$1+'arpal</span>');

NOTES:

  • In the search term:
    • The [C|c] matches an upper or lower-case character. The parentheses surrounding it set a back-references. This allows each match to preserve case.
  • In the replace term:
    • Because the HTML code needs to use double-quote, we use single quotes to define the replacement string.
    • Using back-references, the terms (e.g. $1) go outside the quoted parts of the replacement string otherwise they are treated as literal text (e.g. “$1”). thus the replacement string is composed of two quoted strings concatenated with a back-reference.
2 Likes

Thank you - very helpful

1 Like