By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,384 Members | 1,886 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,384 IT Pros & Developers. It's quick & easy.

RegExp assistance?

P: n/a
Hello,

I'm new with Regular Expression. I've been trying to find the right
regular expression to accomplish what I'm trying to do... but without
much success so far.

I understand there are a bunch of "special characters" used for
creating the regular expression.
http://www.devguru.com/technologies/...ript/11284.asp

I'm trying to globally replace all the HTML anchor from the string,
because I'm grabbing the string from innerHTML of an element.

So if I have a table, with bunch of cell (i.e TD) with <A {some
text}>{some more text}</A>, how can I use the string.replace function
with the appropriate regular expression, subsitute the <a, >, and </a>
with blank?

Thank in advance for the help

Kelvin

Sep 13 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a

kelvlam wrote:
Hello,

I'm new with Regular Expression. I've been trying to find the right
regular expression to accomplish what I'm trying to do... but without
much success so far.

I understand there are a bunch of "special characters" used for
creating the regular expression.
http://www.devguru.com/technologies/...ript/11284.asp

I'm trying to globally replace all the HTML anchor from the string,
because I'm grabbing the string from innerHTML of an element.

So if I have a table, with bunch of cell (i.e TD) with <A {some
text}>{some more text}</A>, how can I use the string.replace function
with the appropriate regular expression, subsitute the <a, >, and </a>
with blank?

Thank in advance for the help

Kelvin
I found my own solution finally!

var printContent = new String(objSource.innerHTML);
// remove all the <abegin tag
printContent = printContent.replace(/<a[^>]*>/g,"");
// remove all the </aend tag
printContent = printContent.replace(/<\/a>/g,"");

Sep 13 '06 #2

P: n/a
You can combine these two statements, if you like, by using a ? with
the "/" character, like this:

replace(/<\/?a[^>]*>/g,"")

Stan Scott
New York City
I'm trying to globally replace all the HTML anchor from the string,
because I'm grabbing the string from innerHTML of an element.
var printContent = new String(objSource.innerHTML);
// remove all the <abegin tag
printContent = printContent.replace(/<a[^>]*>/g,"");
// remove all the </aend tag
printContent = printContent.replace(/<\/a>/g,"");
Sep 13 '06 #3

P: n/a
kelvlam said the following on 9/13/2006 6:34 PM:
kelvlam wrote:
>Hello,

I'm new with Regular Expression. I've been trying to find the right
regular expression to accomplish what I'm trying to do... but without
much success so far.

I understand there are a bunch of "special characters" used for
creating the regular expression.
http://www.devguru.com/technologies/...ript/11284.asp

I'm trying to globally replace all the HTML anchor from the string,
because I'm grabbing the string from innerHTML of an element.

So if I have a table, with bunch of cell (i.e TD) with <A {some
text}>{some more text}</A>, how can I use the string.replace function
with the appropriate regular expression, subsitute the <a, >, and </a>
with blank?

Thank in advance for the help

Kelvin

I found my own solution finally!

var printContent = new String(objSource.innerHTML);
// remove all the <abegin tag
printContent = printContent.replace(/<a[^>]*>/g,"");
Be aware though that if the A anchor has any string in it that contains
then you wont strip the entire A element. onmouseover="if(a>b)" or
similar then it will stop at that first >

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Sep 13 '06 #4

P: n/a

Randy Webb wrote:
kelvlam said the following on 9/13/2006 6:34 PM:
kelvlam wrote:
Hello,

I'm new with Regular Expression. I've been trying to find the right
regular expression to accomplish what I'm trying to do... but without
much success so far.

I understand there are a bunch of "special characters" used for
creating the regular expression.
http://www.devguru.com/technologies/...ript/11284.asp

I'm trying to globally replace all the HTML anchor from the string,
because I'm grabbing the string from innerHTML of an element.

So if I have a table, with bunch of cell (i.e TD) with <A {some
text}>{some more text}</A>, how can I use the string.replace function
with the appropriate regular expression, subsitute the <a, >, and </a>
with blank?

Thank in advance for the help

Kelvin
I found my own solution finally!

var printContent = new String(objSource.innerHTML);
// remove all the <abegin tag
printContent = printContent.replace(/<a[^>]*>/g,"");

Be aware though that if the A anchor has any string in it that contains
then you wont strip the entire A element. onmouseover="if(a>b)" or
similar then it will stop at that first >

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Thanks Stan, I also realized because I'm using XSLT transformation,
adding the "i" flag would make it work for both Firefox and IE. Now I
can do it all in 1 line Javascript instead of 2.

Randy pointed out a a good caveat to watch out for. Fortunately any
javascript event or HTML attribute of the <Atag doesn't contain the
">" in my scenario. Or else...

Much appreciate the responses.

Kelvin

Sep 14 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.