473,434 Members | 1,542 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,434 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 1997
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: 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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.