/    Sign up×
Community /Pin to ProfileBookmark

Replacing the ">" portion of each <td> tag with ">__".

If anyone can assist with this issue I’m having I would greatly appreciate it, greatly! I simply want to replace every <TD> tag with <TD>__ while preserving any attributes within the tag, as well as the content it surrounds.

Here are the results I’m getting so far. Though it works as desired, the attributes within the TD tags are being stripped out. The attributes such as class, height, etc, need to stay upon the flagTags function being ran.

I only want to replace the “>” portion of each <td> tag with “>__”.

Thank you in advance!

[CODE]<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”
“http://www.w3.org/TR/html4/loose.dtd”>
<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=iso-8859-1″>
<title>Untitled Document</title>
<script type=”text/JavaScript”>

function flagTags(){

var content = document.myform.code.value;
var re= new RegExp(‘<td[^><]*>|<td[^><]*>’,’g’)
content = content.replace(re,'<td>__’);
document.myform.code.value = content;
}
function unflagTags(){

var content = document.myform.code.value;
content = content.replace(/<td>__/g,'<td>’);
document.myform.code.value = content;
}

</script>

</head>

<body>a
<form name=”myform”>
<textarea name=”code” cols=”100″ rows=”9″ value=””><table border=”1″ width=”100%” id=”table1″>
<tr>
<th>&nbsp;</th>
<td class=’something’>&nbsp;</td>
<td class=’something’>Some Content</td>
<td>Some More Content</td>
<td></td>
<td></td>
<td></td>
</tr>
</table>
</textarea>
<br>
<input type=”button” value=”Add TD Flags” onclick=”flagTags()”>
<br>
<input type=”button” value=”Remove TD Flags” onclick=”unflagTags()”>
<br>
<input type=”reset” value=”Reset”>
</form>
</body>
</html>[/CODE]

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@OverstatementJan 12.2007 — Without reading any of your code, I'm going to suggest this Regular Expression:
<i>
</i>Text.replace(/(&lt;td[^&gt;]*&gt;)/gi, '$1__');

I LOVE RE!

Edit: Ok, so I'm not as lazy as I thought so I read your code. Incase you were wondering (and it's very likely you have no idea these existed) what '$1' means, they are [url=http://www.regular-expressions.info/brackets.html]backreferences[/url] and they are awesome cool at replacing stuff.
Copy linkTweet thisAlerts:
@gregw74authorJan 12.2007 — You are a genius!! Thank you so much!

Now, I just have to ask... what bit of code would I use to reverse this replacement. Knowing how is going to help me preserve all of my TD tags while some other HTML Cleanup code runs between these two functions.

Dang I wish I could comprehend this stuff better!

Thanks again!!
Copy linkTweet thisAlerts:
@OverstatementJan 12.2007 — Damn, you posted before I edited it. Oh well, here you go.
Don't forget that HTML is case insentive. Also don't forget to use the same-ish method on reverting your <td> tags back to normal
<i>
</i>Text.replace(/(&lt;td[^&gt;]*&gt;)__/gi, '$1');

[/quote]
Copy linkTweet thisAlerts:
@gregw74authorJan 12.2007 — Based on your brilliant suggestion to get my script working, thanks again!.... I am using the following function to reverse the process. Seem ok??

[CODE] function unflagTags(){

var content = document.myform.code.value;
content= content.replace(/>__/gi, '>');
document.myform.code.value = content;
}[/CODE]
Copy linkTweet thisAlerts:
@OverstatementJan 12.2007 — While I do agree with you that I'm a genius, I can't take credit for a function of RE.

Yes, it looks fine assuming you aren't going to do something unexpected like replacing more than just <TD> tags because that code will obviously replace any tag with the __ after it. Also, you don't need the case insentivity on if you are using stuff that don't have an opposite case.
Copy linkTweet thisAlerts:
@gregw74authorJan 12.2007 — I used the code you suggested, much more trustworthy than what I had. It worked like a champ! ? Thank you!!

Given that you've proven yourself as a "white wizard" of regular expressions; if you're game, I have one other question for you, or anyone who's willing for that matter. It should be much simpler. I don't want to wear out my welcome though, if you know what I mean.

If you're up to it, you can find my latest and greatest post here. Yeah, real exciting I know:

http://www.webdeveloper.com/forum/showthread.php?p=696041#post696041
×

Success!

Help @gregw74 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...