In the dataframe below, I have multiple strings stored:
v1 v2
ARSTNFGATTATNMGATGHTGNKGTEEFR SEQUENCE1
BRCTNIGATGATNLGATGHTGNQGTEEFR SEQUENCE2
ARSTNFGATTATNMGATGHTGNKGTEEFR SEQUENCE3
I am interested in searching for and highlighting specific substrings within each string in column v1. For instance, I want to find substrings where the first letter is "N" and the last letter is "G", with a variable middle letter like "NAG" or "NBG" up to "NZG". Instead of writing 26 lines of code as shown below to highlight these three-character substrings, I wish to optimize the process. While I'm new to JavaScript, I aim to improve this code in my R Shiny tab. If there are any areas that need clarification, please ask before giving negative feedback.
ARSTNFGATTATNMGATGHTGNKGTEEFR
BRCTNIGATGATNLGATGHTGNQGTEEFR
ARSTNFGATTATNMGATGHTGNKGTEEFR
Below is an excerpt from the code, representing the first and last line out of the 26 lines I use:
datatable(DF, options = list(rowCallback=JS("function(row,data) {
data[0] = data[0].replace(/NAG/g,'<span style=\"color:blue; font-weight:bold\">NAG</span>');
.....
data[0] = data[0].replace(/NZG/g, '<span style=\"color:blue; font-weight:bold\"\">NZG</span>');
$('td:eq(0)', row).html(data[0]);}"), dom = 't'))