Regular expression help

F

FrankIsHere

How can I strip out the text from within this "href" attribute?

Here is the input:
<span href="/test.htm&parameter=2"></span>

What I'd like to get back is:
/test.htm&parameter=2


Thanks!
Frank
 
E

Evertjan.

wrote on 20 jan 2006 in comp.lang.javascript:
How can I strip out the text from within this "href" attribute?

Here is the input:
<span href="/test.htm&parameter=2"></span>

What I'd like to get back is:
/test.htm&parameter=2

t = '<span href="/test.htm&parameter=2"></span>'

t = t.replace(/^.*"(.*)".*$/,'$1')
 
L

Lasse Reichstein Nielsen

How can I strip out the text from within this "href" attribute?

Here is the input:
<span href="/test.htm&parameter=2"></span>

What I'd like to get back is:
/test.htm&parameter=2

Well, that's easy:

function getHref(input) {
return "/test.htm&parameter=2";
}

But seriously, you should be very careful to know exactly what can
vary in the input. Is it always a span element (unlikely, since
they don't have href attributes)? Is it always the href attribute
you need? And is it always contained in double quotes?

Let's assume that you are looking for just one href attribute where
the value is in double quotes. Then the following regular expression
will capture that:

var re = /\bhref="([^"]*)"/;

In a function it would be:
function getHrefRE(input) {
var re = /\bhref="([^"]*)"/;
var match = input.match(re);
if (match) {
return match[1];
}
}


If the quotes can be either single and double quote, a regexp might
be:
var re2 = /\bhref=(['"])([^\1]*)\1/;
where the content is match[2].


Good luck
/L
 
R

RobG

How can I strip out the text from within this "href" attribute?

Here is the input:
<span href="/test.htm&parameter=2"></span>

What I'd like to get back is:
/test.htm&parameter=2

Normally you can just get the attribute value:

alert( refToSpan.href ); // Shows /test.htm&parameter=2


But span elements don't have href attributes so it will not work
reliably - some browsers do not let you access invalid attributes. You
could use an A element:


<a href="/test.htm&parameter=2" name="theLink"></a>

<script type="text/javascript">
alert( document.links[0].href );
</script>
 
M

mick white

How can I strip out the text from within this "href" attribute?

Here is the input:
<span href="/test.htm&parameter=2"></span>

What I'd like to get back is:
/test.htm&parameter=2
output="/"+input.split("/")[1]

Mick
 
E

Evertjan.

mick white wrote on 20 jan 2006 in comp.lang.javascript:
How can I strip out the text from within this "href" attribute?

Here is the input:
<span href="/test.htm&parameter=2"></span>

What I'd like to get back is:
/test.htm&parameter=2
output="/"+input.split("/")[1]

gives:

/test.htm&parameter=2"><
 
F

FrankIsHere

Thanks guys!

The reason I use it in a span tag and not an <a> tag is because of
custom XSL parsing I do on my web application, but that is irrelevant
to the problem.

I have been doing it this way and it seems to work so far, but I'm not
a javascript guy so I don't know if there could be any potential
problems with the way I'm doing it. What do you guys think?

Here's what I'm using:

function stripHref(tag) {
test2 = tag.replace("<span href=","");
last = test2.replace("></span>","");

//alert(last);
if (last.match(/^'.*'$/) || last.match(/^".*"$/)) {
return last.substring(1,last.length-1);
}
return last;
}


-Frank
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated Fri, 20
Jan 2006 14:10:13 remote, seen in Evertjan.
mick white wrote on 20 jan 2006 in comp.lang.javascript:
How can I strip out the text from within this "href" attribute?

Here is the input:
<span href="/test.htm&parameter=2"></span>

What I'd like to get back is:
/test.htm&parameter=2
output="/"+input.split("/")[1]

gives:

/test.htm&parameter=2"><

However, output = input.split('"')[1] seems likely to do what is in
general necessary.
 

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
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top