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

Regular expression help

P: n/a
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

Jan 19 '06 #1
Share this Question
Share on Google+
8 Replies


P: n/a
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')

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jan 19 '06 #2

P: n/a
Fr*********@gmail.com writes:
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
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jan 20 '06 #3

P: n/a
Fr*********@gmail.com wrote:
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>

--
Rob
Jan 20 '06 #4

P: n/a
Fr*********@gmail.com wrote:
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

Jan 20 '06 #5

P: n/a
mick white wrote on 20 jan 2006 in comp.lang.javascript:
Fr*********@gmail.com wrote:
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"><

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jan 20 '06 #6

P: n/a
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

Jan 20 '06 #7

P: n/a
Evertjan. wrote:
mick white wrote on 20 jan 2006 in comp.lang.javascript:

Fr*********@gmail.com wrote:
<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"><

oops
Mick
Jan 20 '06 #8

P: n/a
JRS: In article <Xn********************@194.109.133.242>, dated Fri, 20
Jan 2006 14:10:13 remote, seen in news:comp.lang.javascript, Evertjan.
<ex**************@interxnl.net> posted :
mick white wrote on 20 jan 2006 in comp.lang.javascript:
Fr*********@gmail.com wrote:
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.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jan 21 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.