0

In the following string, i would like to replace [choice:a3d] with an appropriate drop down menu. I am not sure of how the options need to be formatted just after the colon and before the closing square brace.

string = "operation [number] [choice:a3d] [number]";

I am not really sure where the .replace function comes from but the code I am working with has jquery imported.

string.replace(/(?:\[choice\:)(\w+)(?:\])/g, choice_func);

where:

function choice_func(choice_lists, listname, default_opt)  
{  
   console.log("choice_lists: "+choice_lists);  // [choice:a3d]  
   console.log("listname: "+listname);          // a3d
   console.log("default_option: "+default_opt); // 67

   var list = choice_lists[listname];
   return '<span class="string ' + listname + ' autosocket"><select>' + 
   list.map(function(item)
   {  
       if (item === default_opt){
           return '<option selected>' + item + '</option>';
       }else{
           return '<option>' + item + '</option>';
       }
   }).join('') +'</select></span>';  
}

needless to say the code fails with error "Uncaught TypeError: Cannot call method 'map' of undefined"

also where do parameters to the function come from?

Don't assume that any of this code is correct....

3
  • The question is a bit confusing, do you think you can create a jsbin example with what you're trying to achieve? Commented Jul 24, 2011 at 20:52
  • Does this help? jsbin.com/asisur/edit Commented Jul 24, 2011 at 21:31
  • 2
    I recommend you not to code like this. Remember my recommendation when you get back to this code after 3 years. Commented Jul 24, 2011 at 21:33

1 Answer 1

1

This looks to me like it would be simpler for you to just use whatever code you need to use to compute the replacement string and then just do a replace using the string instead of the regex function. Regex functions are best used when you need to examine the context of the match in order to decide what the replacement is, not when you are just doing a replacement to something that can be computed beforehand. It could be made to work that way - there's just no need for that level of complexity.

When using regex callbacks, the callback gets multiple parameters - the first of which is the match string and there are a number of other parameters which are documented here. Then, you must return a string from that function which is what you want to replace it with. You function is pretending that it has three parameters which it does not and thus it won't work.

I suggest that you compute the replacement string and then just do a normal text replacement on it with no regex callback function.

If you can be clearer about what the initial string is and what you want to replace in it, we could give you some sample code that would do it. As you've shown in your question, your string declaration is not even close to legal javascript and it's unclear to me exactly what you want to replace in that string.

The pseudo code would look like this:

var menuStr = "xxxxxxx";
var replaceStr = choice_func(lists, name, options);
menuStr = menuStr.replace(/regular expression/, replaceStr);
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.