2

I have a block of HTML stored in a variable called address_form, within that block of HTML I want to remove, or replace, a portion of it. The part I want to replace is a div with an ID of address_container.

There's clearly something wrong with my RegEx here that i'm using with the replace function as it is not working:

var tempStr = address_form.replace('/\<div id=\"#address_container\"\>.*<\/div\>/', '');

I simply want to replace a string, within a string.

7
  • please post the block of HTML Commented Nov 14, 2012 at 14:52
  • 1
    You should at least remove the single quotes wrapping the regex, because it will be a string and not a regex :-) Commented Nov 14, 2012 at 14:53
  • 1
    If you want to modify HTML code with JavaScript, why not insert it into an element outside of the document and do the modifications via DOM methods? Commented Nov 14, 2012 at 14:53
  • @Sirko was thinking the same thing..and if u need the content as a string you could just get the content by saying outerHTML Commented Nov 14, 2012 at 14:55
  • 1
    Correctly matching a DIV element, (which itself may contain other DIV elements), using a single JavaScript regex is impossible. This is because the js regex engine does not support matching nested structures. Commented Nov 14, 2012 at 15:48

4 Answers 4

2

Since you've tagged your question with jQuery, then I would suggest you use jQuery to do this task. Something like:

var tempStr = jQuery(address_from).remove('#address_container').html();
Sign up to request clarification or add additional context in comments.

2 Comments

+1 - that's how I'd do it, but I know more about the Twilight series than I do regular expressions. I've never seen any of the Twilight series.
+1, that's the real solution. I would remove the .html() at the end and work with the jQuery object, though.
0

Don't do that, just get the contents of the div and replace the parent of that div with the contents.

So

var tempStr = $('#address_container').html(); // or text()
$('#parent_of_address_container').html(tempStr);

Comments

0

Your regex is wrong. Use this instead:

address_form.replace(/<div id=["-]#address_container["-]>.*<\/div>/,'');

Comments

-1

From @RidgeRunner

Correctly matching a DIV element, (which itself may contain other DIV elements), using a single JavaScript regex is impossible. This is because the js regex engine does not support matching nested structures.

5 Comments

@Simon, it's not a solution - but it's an answer to the question I posed.
You didn't really pose a question, just stated a problem. The solution to your problem would be the one Brian Ball posted.
Just to clear that, I voted you down because if someone, being in the same position as you, is looking for a solution here, he would just find that there seems to be none while there actually is one.
@Simon Disagree. I tried Brian Ball's solution and it didn't quite work, plus I did ask RidgeRunner to add the answer.
So you just go with "not possible" instead of trying to implement Brian Ball's code correctly? It probably would help if you could post what exact string your ´address_form´ is, to figure out why Brian's code didn't work at first try...

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.