473,414 Members | 1,628 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

Regular expression help

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
8 1995
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Reckless | last post by:
I've got a file with this in it: The data I'd like extracted is within the quotes: Some string data I can read the file out and extract (using string positions) the data I'd like but it...
5
by: Ian Richardson | last post by:
I'm looking to use Javascript to pull apart a page of HTML I have already fetched. The page contains a table, within which there are rows containing... Either: 0000000 - 0000000<some...
6
by: JohnSouth | last post by:
Hi I've been using a Regular expression to test for valid email addresses. It looks like: \w+(\w+)*@\w+(\w+)*\.\w+(\w+)* I've now had 2 occassions where it has rejected and email address...
6
by: dotnetprogram | last post by:
Does anybody have a regular expression to be used in the validation controls of asp.net that checks if the text inputted is: 1) alphanumeric and doesn't include special characters and symbols...
9
by: Schorschi | last post by:
Not having used regular expressions much, I need some help. Given a string... "This\0Guy\0Needs\0Some\0Help\0\0\0\0\0" Need result as array of strings... "This","Guy", "Needs", "Some", "Help" ...
3
by: rodchar | last post by:
hey all, what would my expression look like if i wanted to make sure that the input matched the following pattern. c:\filename.ext it doesn't have to be the c drive just a letter, colon,...
3
by: Zach | last post by:
Hello, Please forgive if this is not the most appropriate newsgroup for this question. Unfortunately I didn't find a newsgroup specific to regular expressions. I have the following regular...
14
by: Chris | last post by:
I need a pattern that matches a string that has the same number of '(' as ')': findall( compile('...'), '42^((2x+2)sin(x)) + (log(2)/log(5))' ) = Can anybody help me out? Thanks for any help!
9
by: Rene | last post by:
I'm trying to basically remove chunks of html from a page but I must not be doing my regular expression correctly. What i'm trying with no avail. $site = preg_replace("/<!DOCTYPE(.|\s)*<div...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.