/    Sign up×
Community /Pin to ProfileBookmark

Help with RegEx?

Hi all,

1)
———————————————————————-

var str = “<td charts=”ExcelGraph1,ExcelGraph2″ formula=”1″ standard=”” style=”border-right: 1px solid rgb(228, 227, 201); border-bottom: 1px solid rgb(228, 227, 201); background-color: rgb(255, 255, 255); color: rgb(0, 0, 0); font-weight: normal; font-style: normal; text-decoration: none; font-family: Arial; font-size: 10pt; text-align: left;” class=”cell” id=”cell-4-2″ cellvalue=”1″><div class=”nowrap”>1</div></td>”;

From the above string, I need to replace the charts attribute with “” using RegEx, I tried like this,

str = str.replace(/charts.*ExcelGraph.*s/g, “”);

But I couldn’t find any solution, can anyone help me with a better Solution?
———————————————————————-

2)
————————————————————————

str = “<table><img style=”display:none;” class=”charts” id=”ExcelGraph1″ src=”/test.chart/jfreechart2999.png” height=”200″ width=”300″>
<img id=”spacer” src=”spacefr.gif”><img style=”display:none;” class=”charts” id=”ExcelGraph2″ src=”/test.chart/jfreechart2930.png” height=”200″ width=”300″ ><table></table>”;

From the above string, I need to remove the img tags with id starting with “ExcelGraph” using RegEx, I tried like this,

str = str.replace(/<img.*id.*ExcelGraph.*>/gi, “”);

But I couldn’t find any solution, can anyone help me with a better Solution?
———————————————————————-

Thanks & Regards,
Suresh.

to post a comment
JavaScript

12 Comments(s)

Copy linkTweet thisAlerts:
@phpnoviceJun 16.2006 — 1)

str = str.replace(/ charts="[^"]+"/, "");

...or...

str = str.replace(/ charts="ExcelGraph[^"]+"/, "");
Copy linkTweet thisAlerts:
@ssureshkumarauthorJun 17.2006 — Hi,

Thanks for your quick response,

I got it right for the first one.

could u please help me with the second one?

Thanks & Regards,

Suresh.
Copy linkTweet thisAlerts:
@KorJun 17.2006 — Will I be excused, but I feel that if you had have used DOM methods to create your elements (table, tr, td, img) it would have been extremely easy to change elements' attributes' values later.

On the other way, it looks to be that your HTML elements in your strings are not correctly written. A table must have row/rows and cell/cells. A sequence <table> - <img> - </table> looks incomplete and incorrect to me.
Copy linkTweet thisAlerts:
@ssureshkumarauthorJun 17.2006 — Hi Kor,

Thanks for your response.

I have given only the sample htmlstring.

Actually, its a large html file. From that I have to remove the image tags.

Hope u understand.

Regards,

Suresh.
Copy linkTweet thisAlerts:
@phpnoviceJun 17.2006 — could u please help me with the second one?[/QUOTE]
It is the same concept/technique as the one I've already done for you. Figure out what I did for that one and you can apply that knowledge to the second one.
Copy linkTweet thisAlerts:
@ssureshkumarauthorJun 17.2006 — Hi,

thanks for your help.

I solved the problem.

Here is my solution to remove the corresponding image tag,

replacedValue = replacedValue.replace(/<img.*id="ExcelGraph[^"]+"[^<]+/gi, "");

It produced me the correct result.

I need to clarify, whether this solution works in all the conditions.

Thanks for any response,

Regards,

Suresh.
Copy linkTweet thisAlerts:
@phpnoviceJun 17.2006 — Excellent! Good going.

Cheers.
Copy linkTweet thisAlerts:
@KorJun 17.2006 — ... I stiil would have used DOM to solve this kinda...
Copy linkTweet thisAlerts:
@phpnoviceJun 17.2006 — ... I stiil would have used DOM to solve this kinda...[/QUOTE]
Yeah, so tell me... When you receive the entire HTML source of a page as a string of text, and you wish to selectively modify parts of that text string... How would use the DOM to make that modification to the text string? The only way I know of is to have the browser first render the unmodified text and, then, use the DOM (after the fact) to modify the rendered HTML. I don't think that is as good of a solution -- though it would certainly work just fine (harder to find the desired elements, though).
Copy linkTweet thisAlerts:
@KorJun 17.2006 — make it an XML from the begining
Copy linkTweet thisAlerts:
@phpnoviceJun 17.2006 — Now you're changing the whole setup. This would cause a major conversion of all existing HTML-only source to XML. Not very efficient. :rolleyes:
Copy linkTweet thisAlerts:
@KorJun 18.2006 — Yes, I know, it is another discution. You are right, as long as ssureshkumar has already a string...
×

Success!

Help @ssureshkumar 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 5.4,
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: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

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