473,396 Members | 1,871 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Regex / replace html tags.

Jezternz
145 100+
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:

Expand|Select|Wrap|Line Numbers
  1. function convert_body(body){
  2.      var newbody = body.replace(/[>]/gi, ' onclick="select(this)">');
  3.      return newbody;
  4. }
  5.  
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]
Mar 27 '08 #1
9 2764
acoder
16,027 Expert Mod 8TB
See this link (grabbing HTML tags).
Mar 27 '08 #2
Jezternz
145 100+
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:
Expand|Select|Wrap|Line Numbers
  1. execute_all_children('document.body');
  2.  
  3. function execute_all_children(element){
  4.     var elementdir = eval(element);
  5.      if(elementdir.nodeType == 1){
  6.          // Do changes to every element (background in this eg)
  7.           elementdir.style.backgroundColor = 'black';
  8.     }             
  9.      var i = 0;
  10.      while(i<elementdir.childNodes.length){        
  11.         execute_all_children(element+'.childNodes['+i+']');    
  12.         i++;    
  13.     }
  14. }
  15.  
Mar 28 '08 #3
acoder
16,027 Expert Mod 8TB
So you used recursion. That's all good. It is possible though to capture the text using parentheses.
Mar 31 '08 #4
Jezternz
145 100+
is it more effiecient? could you start me off if it is? cheerz
Apr 2 '08 #5
acoder
16,027 Expert Mod 8TB
How about:
Expand|Select|Wrap|Line Numbers
  1. body.replace(/<([A-Z][A-Z0-9]*)\b([^>]*)>/gi, '<$1 $2 onclick="select(this)">');
It matches the opening tag and the rest of the content before it closes and then uses backreferences from the capturing parentheses.
Apr 2 '08 #6
Jezternz
145 100+
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.
Apr 2 '08 #7
acoder
16,027 Expert Mod 8TB
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.
Apr 2 '08 #8
Jezternz
145 100+
oh ok, that now makes alot more sence to me. thats cool, thanks acoder!
Apr 3 '08 #9
acoder
16,027 Expert Mod 8TB
In fact, looking back at it, you can match the whole opening tag in one go:
Expand|Select|Wrap|Line Numbers
  1. body.replace(/<([A-Z][A-Z0-9]*\b([^>]*)>/gi, '<$1 onclick="select(this)">');
but anyway, you're welcome.
Apr 4 '08 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: mike c | last post by:
I have a search app that searches local HTML files for a specified term. I then display the pages that contain the term. I would like to highlight the search term within the HTML when it is...
3
by: DDK | last post by:
I am trying to figure out how to Replace tags such as ... with the correct HTML <b>...</b> tags in C#. The code below works however only if one set of tags are found, if you have more than two...
7
by: Rocky Moore | last post by:
I have a web site called HintsAndTips.com. On this site people post tips using a very simply webform with a multi line TextBox for inputing the tip text. This text is encode to HTML so that no...
1
by: darrel | last post by:
I have some vb.net code that is running a regex, matching groups, and replacing them. I'm trying to come up with a simple script that will strip all attributes from all HTML tags. This is what I...
2
by: Tim_Mac | last post by:
hi, i have a tricky problem and my regex expertise has reached its limit. i have read other posts on this newsgroup that pull out the plain text from a html string, but that won't work for me...
3
by: Razvan | last post by:
Hello there, I have the following problem: I have a big html and i want to remove from it everything between some tags and to keep the rest, of course using regex, but any solution will be...
3
by: Rob | last post by:
Hi, I've written a small VB application that parses an HTML document and removes code I don't need and re-writes the file. I'm looking for the regex pattern that will remove the following code: ...
1
by: jonnyboy6969 | last post by:
Hi All Really hoping someone can help me out here with my deficient regex skills :) I have a function which takes a string of HTML and replaces a term (word or phrase) with a link. The pupose...
0
by: Karch | last post by:
I have these two methods that are chewing up a ton of CPU time in my application. Does anyone have any suggestions on how to optimize them or rewrite them without Regex? The most time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.