1

I have a start and end marker in html, I would like to replace the html between these markers with some other html. The expression below doesnt seem to work...

var myRegExp = new RegExp("<!-- " + "START IMPORTED HEAD.*?<!-- " + "END IMPORTED HEAD -->", "gi");
sHtmlContent = sHtmlContent.replace(myRegExp, "<!-- " + "INSERT_HEADER -->");

below is an example of the html block which appears in doc.

<!-- START IMPORTED HEAD -->
<style type="text/css">   
    input[type=button]
    {       
        padding-bottom: 3px;
    }        
</style>
<!-- END IMPORTED HEAD -->

any ideas would be great thanks

3
  • 6
    You should be using DOM manipulation for this. Don't know what the DOM is? Go learn about it! Now! ... You're back? Using the DOM seems like a lot of work for something so simple? Go learn jQuery! ;) Commented Feb 7, 2011 at 15:42
  • ok how would you suggest I do it via the DOM? thanks great help Commented Feb 7, 2011 at 15:45
  • looking for a regex based solution cheers Commented Feb 7, 2011 at 15:50

3 Answers 3

2

Manipulating the DOM is much better, but only if you have a browser. If you're doing server-side JavaScript (e.g. JS ASP) here's a way to do exactly what you asked:

var html = '<!-- START IMPORTED HEAD -->\n<style type="text/css">\n    input[type=button]\n    {\n        padding-bottom: 3px;\n    }\n</style>\n<!-- END IMPORTED HEAD -->';
var re   = new RegExp("<!-- " + "START IMPORTED HEAD[\\d\\D]*?<!-- " + "END IMPORTED HEAD -->", "gi");

html.replace(re, "<!-- " + "INSERT_HEADER -->");
// <!-- INSERT_HEADER -->

Your problem is that in JS the . character does not match newlines; using a [\d\D] character class instead matches all characters.

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

1 Comment

Alternatively, JavaScript supports a negated empty character class for the same effect: [^]
2

The problem are the newlines in your doc. Javascript does not support multiline (dot match all) matching out of the box. Either you remove the newlines first or you use an extension like XRegexp

See here: regexpal and then click the Dot matches all.

Comments

1

Using the DOM is a much more robust solution to your problem. Instead of using comment markers, just place that section of HTML inside a div with a known id:

<div id="foo">Original things</div>

With jQuery, you can do the replacement in a single line:

$('div#foo').html('Replaced things')

This will change the content of the div into:

<div id="foo">Replaced things</div>

2 Comments

@keyoke You posted that you are looking for a regex-based solution and then you accept this? :)
thanks for your reply as I did ask for a regex based solution, I have changed the correct answer I selected, but both seem to work in my situation

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.