0

I am trying to replace <h[num] id="....">Header</h[num]> with h[num]. Header

This is what I have tried, but I cannot get it to work:

html.replace('/<h(\d+).*?>([\s\S]*?)<\/h.*?>/', "h$1. $2");

Cheetah
  • 13,785
  • 31
  • 106
  • 190
  • 2
    All is lost [the pony he comes](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454)! – Jongware Dec 02 '13 at 14:00
  • @Jongware - If I have confused you too much I apologise. Let me make it simpler for you. Imagine html doesn't exist for a minute. All I am trying to do trying to do is parse the following `Header` into `h[num]. Header`. – Cheetah Dec 02 '13 at 14:57
  • You must not do it with regex, see http://stackoverflow.com/a/1732454/1243636 – akaRem Dec 02 '13 at 15:43
  • @akaRem - If I have confused you too much I apologise. Let me make it simpler for you. Imagine html doesn't exist for a minute. All I am trying to do trying to do is parse the following `Header` into `h[num]. Header`. – Cheetah Dec 02 '13 at 17:31

2 Answers2

0

Actually, it can.

Check the example in http://jsfiddle.net/cLHk3/

function replaceHTML(){
    var elements = document.querySelectorAll('h1,h2,h3,h4,h5');
    Array.prototype.slice.call(elements).forEach(function(element){
        var text = element.tagName+'. Header';     
        element.parentNode.insertBefore(document.createTextNode(text),element);
        element.parentNode.insertBefore(document.createElement('br'),element);
        element.parentNode.removeChild(element);
    })
}
Tom Chung
  • 1,412
  • 9
  • 12
0

The answer to this was the following:

var html = <h1 id="....">Header</h1>
var re = /^<h(\d+)[\s\S]*?>([\s\S]*)<\/h(\d+)>$/;

html.replace(re, "h$1. $2");
Cheetah
  • 13,785
  • 31
  • 106
  • 190