Unable to select some text from Google Docs with a simple regex -


i'm trying highlight text (in example below highlight "organiza") regex on google docs document, i'm unable make first work simple regex find "category_name" string.

why this:

function highlighttexttwo() {    /* document definition */   var doc  = documentapp.openbyid('1m6jmjpndls_hkdauo5holsdxb5gssrcwma1j4hh7dig');    /* variable definition */     var highlightstyle = {};   var paras = doc.getparagraphs();   var textlocation = {};   var i;    /* regex definition */     var myregex = new regexp('category_name','i');        /* color style definition */   highlightstyle[documentapp.attribute.foreground_color] = '#ff0000';      /* code */     (i=0; i<paras.length; ++i) {     logger.log( paras[i].findtext(myregex) );   }   } 

applied document:

{     "map_image": "mapa_con_close_button.png",     "categories":[         {             "category_id": 1,             "category_name": "organiza",             "color": "#4591d0",             "icon_image": "organiza.png"         },         {             "category_id": 2,             "category_name": "delega",             "color": "#94c5dd",             "icon_image": "delega.png"         },         {             "category_id": 3,             "category_name": "negocia y gestiona el conflicto",             "color": "#e7344a",             "icon_image": "negocia_y_gestiona.png"         } 

returns this:

[15-06-03 20:12:48:026 cest] null [15-06-03 20:12:48:027 cest] null [15-06-03 20:12:48:028 cest] null [15-06-03 20:12:48:029 cest] null [15-06-03 20:12:48:030 cest] null [15-06-03 20:12:48:030 cest] null 

instead nulls , 1 "category_name".

i found out way display of category_name strings. main points:

  1. use regexp exec in while loop instead od findtext
  2. to occurrences, need use g flag regex
  3. to access text in paragraphs, need gettext()

code:

var paras = doc.getparagraphs(); var myregex = new regexp('category_name','ig'); (i=0; i<paras.length; ++i) {   while (match = myregex.exec(paras[i].gettext()))   {     logger.log(match[0]);    } } 

output in log:

[15-06-04 21:07:36:320 cest] category_name [15-06-04 21:07:36:322 cest] category_name [15-06-04 21:07:36:324 cest] category_name 

edit:

here way highlight matches red color:

var paras = doc.getparagraphs(); var myregex = new regexp('category_name','ig'); (i=0; i<paras.length; ++i) {     while (match = myregex.exec(paras[i].gettext()))     {         var searchresult = paras[i].findtext(match[0]);           if (searchresult !== null) {             var thiselement = searchresult.getelement();             var thiselementtext = thiselement.astext();             thiselementtext.setbackgroundcolor(searchresult.getstartoffset(), searchresult.getendoffsetinclusive(),"#ff0000");         }      }   } 

Comments

Popular posts from this blog

javascript - Bootstrap Popover: iOS Safari strange behaviour -

Magento/PHP - Get phones on all members in a customer group -

session - Logging Out Using PHP -