Hi,

I'm currently working on a script that parses out function keys in a string of text. I've got this code which works, but was just looking for ideas on how to improve it :)

I would have liked to use regex, but I would need to do variable substitution for the command key text which I don't think is possible. By regex variable substitution I mean something like :
/\s(substitute cmdKeys[aa] here??)\-/

^^ the "\-" at the end is for the "-" character. Not sure if thats corrent though.

An example of the format of the string that i would be parsing is

"F3-Menu F8-Projection F12-Backup F21-Functions"

var str = FIELDS("footer").getValue();
var footer_len = str.length;
var ii = 0;

var cmdKeys = new Array("F1","F2","F3","F4","F5","F6","F7","F8","F9","F10","F11","F12","F13","F14","F15","F16","F17","F18","F19","F20","F21","F22","F23","F24");
var label = '';

var cmdKeys_len = cmdKeys.length;
for( var aa = 0; aa < cmdKeys_len; aa = aa + 1){
   var found = 0;
   var key = cmdKeys[aa];
   var index = str.search(key);
   
   if( (index >= 0) && (str.charAt(index + key.length) == '-') )
      found = 1;

   if( found ){

      var ch = '';
      var jj = index;
   
      while( ' ' != ch ){
         ch = str.charAt(jj);
         label = label + ch;
         jj = jj + 1;
      }

      //get a reference to the button and make it visible
      var F = FIELDS(cmdKeys[aa]);
      F.setProperty("visible", true);
      F.refresh();
   }
}

Thanks!

Recommended Answers

All 5 Replies

Are you looking for a way to use regex to substitute only the portion of the text? You can use match(regex) for any string. If it match, it will return something; otherwise, it returns null. If you add option 'i' to the match as str.match(/str/i), it would ignore case.

For replace, if you want to replace only the first occurrence found, you can just use str.replace(regex_or_string, string).

i.e.

var str = "My string here"
  if (str.match(/str/)) { alert(str.replace(/str/,"STR") }  // result "My STRing here"
  else { alert("Not found 'str'") }

You may read about match() here and about replace() here.

In your case, I assume that you are trying to match an existing string with each of whatever in a predefined array. You may do it as followed:

var str = FIELDS("footer").getValue();  // assume that str is "F3-Menu F8-Projection F12-Backup F21-Functions"
var cmdKeys = new Array("F1","F2","F3","F4","F5","F6","F7","F8","F9","F10","F11","F12","F13","F14","F15","F16","F17","F18","F19","F20","F21","F22","F23","F24");
var label;
var cmdKeys_len = cmdKeys.length;
for( var aa = 0; aa < cmdKeys_len; aa++){
   var found = 0;
   var key = cmdKeys[aa]+"-";
   label = "";
   if(str.match(key)) {  // found the match
     label = str.match(key+"\\w+[^\\s]*")  // match from starting key string to the first white space found
   }
  if (label.length>0) {
    // do something with the label you found
  }
}

Are you looking for a way to use regex to substitute only the portion of the text?

What I meant by variable substitution was being able to use a variable as part of a regex pattern. I don't think I explained myself very well, but you hit the nail on the head with this :

label = str.match(key+"\\w+[^\\s]*")

I'm going to have to modify it a bit to do exactly what I want. I'll post back my solution in a bit.

Thanks a lot!

Just one question-why did you escape the "\" characters?

In javascript, you need to escape the backslash again in order to get it correctly. It takes a string in and it would think that the backslash is going to escape anything right after it. That's why the backslash itself needs to be escaped.

well I've learned a lot about Javascript in the process of writing this script. I found out the hard way that the match() function returns an array. that caused be a lot of headache.

I've ended up doing the following: match(key + "-[^\-]+-")

I think it could be better so that it automatically strips out the the function key, spaces and the '-' character, but my regex skills are pretty basic. I'll work on that when i have time.

Thanks a lot for you help Taywin!

Just in case anyone's interested here's the entire scripts. regex are pretty amazing.

var str = FIELDS("footer").getValue();
var cmdKeys = new Array("F1","F2","F3","F4","F5","F6","F7","F8","F9","F10","F11","F12","F13","F14","F15","F16","F17","F18","F19","F20","F21","F22","F23","F24");
var num_keys = str.split("-").length - 1;//find the number of function keys in the footer

for( var aa = 0; aa < cmdKeys.length; aa += 1){
   var key = cmdKeys[aa];
   var label = '';
   var found = 0;

   var index = str.search( key + "-" );

   if ( index != -1 ){
      num_keys -= 1;
      
      if ( num_keys != 0 ){
         //the hyphen must be used as the terminating character since the label could contain anything, including spaces
         label = str.match( key + "-[^\-]+-" );
      }
      else {
         label = str.match( key + "-.+" );//there won't be a terminating hyphen character at the end of the footer
      }
      label = label.join(); //convert the array to a string

      //the text for the next command key will need to be removed from the text just parsed
      //for the current command key since '-' is the terminating character
      if ( num_keys != 0 ){
         label = label.substring(key.length + 1, label.length - cmdKeys[aa+1].length - 3);
      }
      else {
         //7 should be the number of characters from the end of the footer string to the last blank
         //since the 6 character screen name will always be at the end of the footer
         var spaces = 0;
         var kk = label.length - 7;
         var char = label.charAt(kk);
         //count spaces inbetween the screen name and the label
         while( char == ' ' ) {                                     
            spaces += 1;
            kk -= 1;
            char = label.charAt(kk);
         }
         //get rid of spaces in between the screen name and the label
         label = label.substring(key.length + 1, label.length - 6 - spaces);
      }
      
      //get a reference to the button and make it visible
      var F = FIELDS(cmdKeys[aa]);
      F.setProperty("visible", true);
      F.setProperty("tooltip", label);   
      F.refresh();
   } 
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.