RegExp assistance?

K

kelvlam

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/javascript/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
 
K

kelvlam

kelvlam said:
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/javascript/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 <a> begin tag
printContent = printContent.replace(/<a[^>]*>/g,"");
// remove all the </a> end tag
printContent = printContent.replace(/<\/a>/g,"");
 
S

stannyc

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
var printContent = new String(objSource.innerHTML);
// remove all the <a> begin tag
printContent = printContent.replace(/<a[^>]*>/g,"");
// remove all the </a> end tag
printContent = printContent.replace(/<\/a>/g,"");
 
R

Randy Webb

kelvlam said the following on 9/13/2006 6:34 PM:
kelvlam said:
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/javascript/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 <a> begin 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 >
 
K

kelvlam

Randy said:
kelvlam said the following on 9/13/2006 6:34 PM:
kelvlam said:
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/javascript/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 <a> begin 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 <A> tag doesn't contain the
">" in my scenario. Or else...

Much appreciate the responses.

Kelvin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top