473,804 Members | 3,809 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Regular expression help

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

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

What I'd like to get back is:
/test.htm&parame ter=2
Thanks!
Frank

Jan 19 '06 #1
8 2034
wrote on 20 jan 2006 in comp.lang.javas cript:
How can I strip out the text from within this "href" attribute?

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

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


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

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

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jan 19 '06 #2
Fr*********@gma il.com writes:
How can I strip out the text from within this "href" attribute?

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

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


Well, that's easy:

function getHref(input) {
return "/test.htm&parame ter=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/rasterTriangleD OM.html>
'Faith without judgement merely degrades the spirit divine.'
Jan 20 '06 #3
Fr*********@gma il.com wrote:
How can I strip out the text from within this "href" attribute?

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

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


Normally you can just get the attribute value:

alert( refToSpan.href ); // Shows /test.htm&parame ter=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&parame ter=2" name="theLink"> </a>

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

--
Rob
Jan 20 '06 #4
Fr*********@gma il.com wrote:
How can I strip out the text from within this "href" attribute?

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

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

output="/"+input.spl it("/")[1]

Mick

Jan 20 '06 #5
mick white wrote on 20 jan 2006 in comp.lang.javas cript:
Fr*********@gma il.com wrote:
How can I strip out the text from within this "href" attribute?

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

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

output="/"+input.spl it("/")[1]


gives:

/test.htm&parame ter=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("<s pan 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.javas cript:

Fr*********@g mail.com wrote:
<span href="/test.htm&parame ter=2"></span>

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


output="/"+input.spl it("/")[1]


gives:

/test.htm&parame ter=2"><

oops
Mick
Jan 20 '06 #8
JRS: In article <Xn************ ********@194.10 9.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.javas cript:
Fr*********@gma il.com wrote:
How can I strip out the text from within this "href" attribute?

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

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

output="/"+input.spl it("/")[1]


gives:

/test.htm&parame ter=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.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
<URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demo n.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
2252
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 would neater if I use a regular expression. Only problem is I've never seen a working example of this type of extraction and am completely new to PHP.
5
1480
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 html>00<some html>0 or:
6
489
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 with a "&" character in the local part. I know I should be able to work it out myself, but I'd like to ask anyone to suggest the best way to
6
1306
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 2) but allows multilanguage characters or unicode characters I would prefer it to the format of character set/ class.My head is totally messed up and dizzy looking for this regex. Thanks in advance.
9
1793
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" Where '\0' is a literal zero-byte. I think I need two regular expressions? One to strip the multiple instances of '\0' bytes, and another to split the string.
3
1445
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, filename, and extension.
3
2568
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 expression. ^(.+?) uses (?!a spoon)\.$
14
2279
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
2497
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 class=\"notice_tan\">(.| \s)*</div>/", "", $site); I'm trying to remove from the very top to a specific div Top of file:
0
9706
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10325
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10315
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10075
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9140
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5519
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4295
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3815
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2990
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.