How to select a substring containing a random character between two known characters using javascript?

I have a rowset in a data frame as shown below.

v1 v2 ARSTNFGATTATNMGATGHTGNKGTEEFR SEQUENCE1 BRCTNIGATGATNLGATGHTGNQGTEEFR SEQUENCE2 ARSTNFGATTATNMGATGHTGNKGTEEFR SEQUENCE3 

I want to search and highlight some selected substrings inside each row in column v1. For example, assuming that the first letter in a substring looks like "N" and the last letter looks like "G", and the middle letter can be any letter, like in "N A G" or "N B G" or "N C G" or "N D G", etc. To highlight a three-character substring, as shown below, I write 26 lines of code to display on the Shiny R tab, suggesting that there can be any of 26 letters between “ N ” and “ G ”. I'm just trying to optimize the code. I am new to JS. I hope I get it. If not up to the vote down, please let me know if you need more explanation or details.

ARST NFG ATTAT NMG ATGHTG NKG TEEFR

BRCT NIG ATGAT Guilders ATGHTG NQG TEEFR

ARST NFG ATTAT NMG ATGHTG NKG TEEFR

Here is an abbreviated code with representative 2 lines (first and last line) of 26 lines of code used.

 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')) 
0
javascript highlighting r shiny datatables
Nov 19 '16 at 20:34
source share
2 answers

I found a simple solution. Maybe it will be useful for someone like me.

 datatable(DF, options = list(rowCallback = JS("function(row,data) { data[0] = data[0].replace(/N[AZ]G/g,'<span style=\"color:blue; font-weight:bold\">$&</span>'); $('td:eq(0)', row).html(data[0]);}"), dom = 't')) 
+1
Nov 23 '16 at 6:23
source share

I think you need a regular expression: / N [AZ] G / g

If you also want it to work in lowercase: / N [A-Za-z] G / g

+2
Nov 19 '16 at 20:39
source share



All Articles