1

I am facing trouble in getting the all occurances of pattern of a string replaced with another string.My string looks like this

"<TD>&nbsp;&nbsp;&nbsp;&nbsp;1.Rule<SPAN> <A style="TEXT-DECORATION: underline" id=RULE#I#000002$000000 class=anchorTag NAME:RULE#I#000002$000000?>xxxx</A>&nbsp;
<SCRIPT>function show(id){document.getElementById(id).style.visibility = "visible";}function hide(id){document.getElementById(id).style.visibility = "hidden";}</SCRIPT>
<NOBR><SPAN style="CURSOR: hand" id=RULE#T#000002$000000NAME:RULE#T#000002$000000 onmouseover="show('RULE#T#000002$000000')" onmouseout="hide('RULE#T#000002$000000',event)" onclick="button_click('RULE#T#000002$000000');" valign="top"><IMG style="VISIBILITY: hidden; CURSOR: hand" id=RULE#T#000002$000000 title="Maintain Rule Title" align=middle src="../../IRM/GBRFFNM/images/text_icon.png" width=12 height=12></SPAN></NOBR><NOBR><SPAN style="CURSOR: hand" id=RULE#A#000002$000000NAME:RULE#A#000002$000000 onmouseover="show('RULE#A#000002$000000')" onmouseout="hide('RULE#A#000002$000000')" onclick="javascript:button_click('RULE#A#000002$000000',event)" valign="top"><IMG style="VISIBILITY: visible; CURSOR: hand" id=RULE#A#000002$000000 title="Add Rule" align=middle src="../../IRM/GBRFFNM/plus.gif" width=12 height=12></SPAN></NOBR></SPAN></TD>" 

I need to replace the id RULE#A#000002$000000 with RULE#A#000003$000000.These ids are dynamic.I searched stack over flow and found that i need to create regular expression.Here is what i did..

var lv_html = parent.innerHTML; ///Contains the string abovwe

  var array = _eleid.split('#');
  var array1 = array[2].split('$');
  lv_id = array1[0] + '$';  // = RULE#A#000002$000000
  var replace_id = stmt_cntr + '$' ; // = RULE#A#000003$000000
  var  html1 =  lv_html.replace(new RegExp( lv_id , 'g'), replace_id);

but i found this was not working. Did i miss something here?

0

1 Answer 1

3

You need to escape the string you're creating your regex from:

function escapeRegExp(str) {
  return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
}

... new RegExp( escapeRegExp(lv_id) , 'g') ...

Your string contains a $, which has a special meaning (end of line) in a regex.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.