-1


I need your help to remove all characters using a Javascript Regex in string HTML Document except <body></body> and whole string inside body tag.

I tried to use this but doesn't work:

var str = "<html><head><title></title></head><body>my content</body></html>"
str.replace(/[^\<body\>(.+)\<\\body\>]+/g,'');

I need the body content only, other option will be to use DOMParser:

var oParser = new DOMParser(str);
var oDOM = oParser.parseFromString(str, "text/xml");

But this throws an error parsing my string document loaded via Ajax.
Thanks in advance for your suggestions!

8
  • 2
    Don't parse HTML with regex in 3.. 2..
    – Marty
    Aug 22, 2014 at 2:42
  • 1
    1.. stackoverflow.com/questions/1732348/… @Marty
    – d'alar'cop
    Aug 22, 2014 at 2:43
  • 1
    @Marty Would you really recommend an HTML parser for something as simple as this? This is not an HTML parsing question; it's a simple matching question.
    – tckmn
    Aug 22, 2014 at 2:45
  • 2
    @Doorknob Actually that is exactly the reason I mentioned that those comments were coming rather than making it myself.
    – Marty
    Aug 22, 2014 at 2:46
  • 1
    @joseluisq: The trouble with all the answers that are using .*? is that it doesn't match newline characters. In place of that, use [\s\S]*?
    – user1106925
    Aug 22, 2014 at 3:01

3 Answers 3

1
var str = "<html><head><title></title></head><body>my content</body></html>"

str=str.match(/<(body)>[\s\S]*?<\/\1>/gi);

//also you can try this:
//str=str.match(/<(body)>.*?<\/\1>/gis);

Regular expression visualization

Debuggex Demo

1
1

You could try this code,

> var str = "<html><head><title></title></head><body>my content</body></html>"
undefined
> str.replace(/.*?(<body>.*?<\/body>).*/g, '$1');
'<body>my content</body>'

DEMO

1
  • 1
    @joseluisq try str.replace(/[\S\s]*?(<body>.*?<\/body>)[\S\s]*/gm, '$1'); Aug 22, 2014 at 2:58
0

You can't (or at least shouldn't) do this with replace; try match instead:

var str = "<html><head><title></title></head><body>my content</body></html>"
var m = str.match(/<body>.*<\/body>/);
console.log(m[0]); //=> "<body>my content</body>"

If you have a multiline string, change the . (which does not include \n) to [\S\s] (not whitespace OR whitespace) or something similar.

4
  • Ok, so how can I do when the string html is more complex regex101.com/r/eJ6sG4/1 ?
    – joseluisq
    Aug 22, 2014 at 2:56
  • 1
    @joseluisq That string has newlines; you'll need the s flag (dotall) for that
    – tckmn
    Aug 22, 2014 at 2:57
  • @Doorknob there isn't a s modifier in js. Aug 22, 2014 at 2:59
  • @AvinashRaj Whoops, that's what I get for answering JS questions while coding in Python. :P I'll edit the answer
    – tckmn
    Aug 22, 2014 at 3:07

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.