tags:

views:

68

answers:

2

Hi There,

I hope there is someone who can help me with this regex

content.match(/<div class="content">.+#-#/ig);

I need to match everything between the <div class="content"> [ Match everything inbetween ] up untill the #-# in the coding

I am using javascript

Thanks

+3  A: 

Try this regular expression:

/<div class="content">[\s\S]+?#-#/ig

[\s\S] will match any character including line breaks and the non-greedy quantifier +? will expand the match to the minimum.

Gumbo
Thanks Gumbo - works 100%
Gerald Ferreira
+1  A: 

Well, strictly speaking:

content.match(/<div class="content">(.+?)##/ig);

Result is every even element in the array. But you really shouldn't be using regexp for this. If you want the content of the 'content' div you should use DOM methods. Along the lines of:

function getContents () {
  var divs = document.getElementsByTagName('DIV');
  var result = [];
  for (var i=0,l=divs.length;i<l;i++) {
    if (divs[i].className == 'content') {
        var content = divs[i].innerHTML;
        result.push(content.match(/(.*?)##/)[1]);
    }
  }
  return result;
}
slebetman
How would I use dom for this? any good links ?
Gerald Ferreira
My usual reference is: https://developer.mozilla.org/en/Gecko_DOM_Reference
slebetman
Thanks for the regex it is working 100% - I am trying the DOM example but get a 'document' is undefined error
Gerald Ferreira
Slebetman - How would I call the function ?<body onload="function getContents()">
Gerald Ferreira
Call it where you would have called the content.match.
slebetman
Finally I figured out why I am using REGEX and not the DOM object - My coding is in xhtml and the DOM in IE8 convert the text to HTML with the inner.html = the resulting code is a total mess! and it converts my xhtml to html
Gerald Ferreira