Regex / replace html tags.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Jezternz
    New Member
    • Jan 2008
    • 145

    Regex / replace html tags.

    Okay!

    I want to make a function where you can input bascly the body of an html page.
    So basicly formatted html. Then I want my function to go through it and add some code to the Open Tags Only (not close tags),
    I would like to just have another replace with regex method and replace the html exit tags, however this is a problem as I do not know what the text inside is.
    Is there anyway I can do this without making my own custom loop? If so please help! if not, I would apreciate a hand starting off the loop. Thanks

    I have made myself a function that does half the work. Only problem is it will do both open and close tags:

    Code:
    function convert_body(body){
     	var newbody = body.replace(/[>]/gi, ' onclick="select(this)">');
     	return newbody;
    }
    Currently if I input
    [HTML]
    <div id="container" >
    <p class="yellow"> text</p>
    <a href="#" class="yellow"> link</a>
    </div>
    [/HTML]
    I get:
    [HTML]
    <div id="container" onclick="select (this)">
    <p class="yellow" onclick="select (this)">text</p onclick="select (this)">
    <a href="#" class="yellow" onclick="select (this)">link</a onclick="select (this)">
    </div onclick="select (this)">
    [/HTML]
    When I want:
    [HTML]
    <div id="container" onclick="select (this)">
    <p class="yellow" onclick="select (this)">text</p>
    <a href="#" class="yellow" onclick="select (this)">link</a>
    </div>
    [/HTML]
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    See this link (grabbing HTML tags).

    Comment

    • Jezternz
      New Member
      • Jan 2008
      • 145

      #3
      yeh I read that however there was a problem because when I searched for closetag with a wildcard text (ie div or a) when I went to replace it I could not retrieve that wildcard text.

      Anyways found a different method to do it:
      Code:
      execute_all_children('document.body');
      
      function execute_all_children(element){
      	var elementdir = eval(element);
       	if(elementdir.nodeType == 1){
      	 	// Do changes to every element (background in this eg)
       	 	elementdir.style.backgroundColor = 'black';
      	}		 	
       	var i = 0;
       	while(i<elementdir.childNodes.length){		
      		execute_all_children(element+'.childNodes['+i+']');	
      		i++;	
      	}
      }

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        So you used recursion. That's all good. It is possible though to capture the text using parentheses.

        Comment

        • Jezternz
          New Member
          • Jan 2008
          • 145

          #5
          is it more effiecient? could you start me off if it is? cheerz

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #6
            How about:
            [code=javascript]body.replace(/<([A-Z][A-Z0-9]*)\b([^>]*)>/gi, '<$1 $2 onclick="select (this)">');[/code]It matches the opening tag and the rest of the content before it closes and then uses backreferences from the capturing parentheses.

            Comment

            • Jezternz
              New Member
              • Jan 2008
              • 145

              #7
              wow gonna have to go back to that other site and look that up, try work out exactly what all that means. thankyou so much acoder, I will defnitly look at replacing my other code. This looks far more efficient.

              Comment

              • acoder
                Recognized Expert MVP
                • Nov 2006
                • 16032

                #8
                It just matches the opening tag only, but with parentheses for re-using in the replace. The $1 and $2 match the parenthesized (captured) input.

                Comment

                • Jezternz
                  New Member
                  • Jan 2008
                  • 145

                  #9
                  oh ok, that now makes alot more sence to me. thats cool, thanks acoder!

                  Comment

                  • acoder
                    Recognized Expert MVP
                    • Nov 2006
                    • 16032

                    #10
                    In fact, looking back at it, you can match the whole opening tag in one go:[code=javascript]body.replace(/<([A-Z][A-Z0-9]*\b([^>]*)>/gi, '<$1 onclick="select (this)">');[/code]but anyway, you're welcome.

                    Comment

                    Working...