Find & Replace Commands

This page uses Hashtag Replace commands to demonstrates altering Variable values.   For case insensitive matching, tag any of these before the search string: case insensitive, insensitive, ci, i.   For regular expression pattern matching, tag any of these before the search string: pattern, regular expression, regex, re.  

Simple String Find & Replace

Processing this Hashtag Markup:
<#

set my_var to "the quick brown fox jumps over the lazy dog";

replace "brown" in my_var with "green";
replace case insensitive "DOG" in my_var with "cat";

#>
Note:  Hashtag Markup is shown below <# in bold #>.   The value generated by processing the Hashtag Markup, and applying any Variable Contexts, is injected into this HTML page after the ⇒ symbol.  
<#[my_var]#> ⇒ the quick green fox jumps over the lazy cat

Regular Expression Find & Replace

Processing this Hashtag Markup:
<#

set my_var to "the quick brown fox jumps over the lazy dog";

replace pattern "(brown|lazy)" in my_var with "nice";

#>
<#[my_var]#> ⇒ the quick nice fox jumps over the nice dog

Referrer Processing

Processing this Hashtag Markup: Remove a GET variable from the referring URL
<# 

// create a copy of the referrer URL, as system values can not be changed
set referrer to "<#[system.referrer]#>";

// leave only the "&" from any &remove=[ anything up to a "&" or the end of URL ]
// $ represents the end of the string
replace pattern "&remove=(.*?)(&|$)" in referrer with "&";

// leave only the "?" from any ?remove=[ anything up to a "&" or the end of URL ]
// ? is a regular expression control character, so it is escaped using \ 
replace pattern "\?remove=(.*?)(&|$)" in referrer with "?";

#>
<#[system.referrer as html]#> ⇒ 
<#[referrer as html]#> ⇒ 


URL Cleansing

Processing this Hashtag Markup:
<# 

set dirty_url to "https://hashtagfoundation.org?&&&var=value&&&message=test&&&";

// use a regular expression pattern to replace duplicate "&" with a single "&"
replace pattern "&+" in dirty_url with "&";

// use a regular expression pattern to remove any trailing "&"
replace pattern "&$" in dirty_url with "";

// use find and replace "?&" with "?"
replace "?&" in dirty_url with "?";

// use regular expression pattern to ensure a trailing "?"
replace pattern "^[^\?]+$" in dirty_url with "<#[dirty_url]#>?";
// dirty_url value should now be https://hashtagfoundation.org?var=value&message=test
#>
<#[dirty_url]#> ⇒ https://hashtagfoundation.org?var=value&message=test


The Hashtag Markup Language provides many methods for setting variable values, and using them later.

Variable Buckets can hold many values, processed together.   Variable Contexts enable safe injection anywhere.