Jump to content
Sign in to follow this  
Greywacke

inserting strings into regex and replacing results with another string

Recommended Posts

hi everyone,

i am currently having an issue with the following excerpt of javascript - particularly the "regular expressions not wanting to replace".

		case 19:	// set where Prim_Att_Val			document.getElementById("ajaxbg").style.visibility = "visible";			var val = document.form_services.where_primattval.options[form_services.where_primattval.selectedIndex].value;		// new constraint			var title = document.getElementById("table_results").tHead.children[1].title;										// current constraints			//salert("re = ^/[ AND]*" + quote(" `Prim_Att_Val` = ""+where["Prim_Att_Val"]+""") + "/g");			//salert("val = "+val);			//salert("title = "+title);			if (whereo["Prim_Att_Val"]==-1) {				document.getElementById("table_results").tHead.children[1].title += ((wherecnt>0)?" AND":"")+" `Prim_Att_Val` = ""+val+""";				where["Prim_Att_Val"] = val;				whereo["Prim_Att_Val"] = wherecnt;				wherecnt++;			} else {				//salert(whereo["Prim_Att_Val"]);				//salert(val);				document.getElementById("table_results").tHead.children[1].title = title.replace(re = new RegExp("/"+quote("`Prim_Att_Val` = "" + where["Prim_Att_Val"]+""")+"/g"),(val!="")?"`Prim_Att_Val` = ""+val+""":val);				if (!where["Prim_Att_Val"]) whereo["Prim_Att_Val"] = -1;				salert(title);				salert(re.toString());				salert("`Prim_Att_Val` = ""+val+""");				salert(whereo["Prim_Att_Val"]);				where["Prim_Att_Val"] = val;			}			document.getElementById("ajaxbg").style.visibility = "hidden";			break; 

salert is a custom function which prints to a div at the top of the screen. same results could be achieved by calling alert, it's just neater with all the data being kept and new lines being written.

what is the issue with line 680 - it is not replacing the values as expected? in case 13, it does however - this does not have the string inserted into the regular expression to search for... anybody, please help!

Share this post


Link to post
Share on other sites

ah - the answer was:

as i was using a compiled regular expression, it was better to pass the modifiers in the second argument of the object ;)

document.getElementById("table_results").tHead.children[1].title = title.replace(re = new RegExp(quote("`Prim_Att_Val` = "" + where["Prim_Att_Val"]+"""),"g"),((val!="")?"`Prim_Att_Val` = ""+val+""":val));

hope this helps many others with similar endeavours! :D

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...