extract substring

S

samuelberthelot

Hi,
I would like to extract 'some string' from the following string:

<A href=\"#\">some string</A>

Attention! This is not an HTML element, it is a STRING, so I can't do
..innerHTML.

'some string' can be of any length.

Can you help ?

thanks
 
R

Richard Cornford

I would like to extract 'some string' from the following string:

<A href=\"#\">some string</A>
<snip>

var string = (' said:
Can you help ?

Hard to say as the real problem has not been defined/stated. Answering
the question asked is unlikely to help much.

Richard.
 
V

Vincent van Beveren

Hi Samuel,

I would like to extract 'some string' from the following string:

<A href=\"#\">some string</A>

substring and indexOf is your friend:

s = "<A href=\"#\">some string</A>";
start = s.indexOf(">"); // get first '>' index
end = s.lastIndexOf("<"); // get last '<' index
// get whats in between, note that you need to add 1
// to start, becuase it gives the location of the symbol
// which is what we want to skip.
link = s.substring(start + 1, end);
alert(link)

Good luck,
Vincent
 
S

samuelberthelot

Thanks Vincent, that works !
Vincent said:
Hi Samuel,



substring and indexOf is your friend:

s = "<A href=\"#\">some string</A>";
start = s.indexOf(">"); // get first '>' index
end = s.lastIndexOf("<"); // get last '<' index
// get whats in between, note that you need to add 1
// to start, becuase it gives the location of the symbol
// which is what we want to skip.
link = s.substring(start + 1, end);
alert(link)

Good luck,
Vincent
 
R

RobG

Hi,
I would like to extract 'some string' from the following string:

<A href=\"#\">some string</A>

Attention! This is not an HTML element, it is a STRING, so I can't do
.innerHTML.

'some string' can be of any length.

If you are simply trying to strip out the HTML tags, then I think:

var s = '<A href=\"#\">some string</A>';
alert( s.replace(/<[^>]*>/g,'') );


Is much simpler. It will strip out *all* tags, so that:

<p>foo <span>bar</span></p>

becomes 'foo bar'.
 

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

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,123
Latest member
Layne6498
Top