• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Extracting data from string

Guest
Apr 19, 2007 Apr 19, 2007

Copy link to clipboard

Copied

Can anyone point me in the right direction as to how URL's can be extracted from a string?
Or at least find the start and end positions...
EG:
I want to get the URL's from the "a href"'s...
String- "The quick brown fox jumps over the lazy dog, <a href=" http://www.example1.com">click here</a>. The quick brown fox jumps over the lazy dog, <a href=" http://www.example2.com">click here</a>.".
TOPICS
Advanced techniques

Views

359

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Apr 19, 2007 Apr 19, 2007

Copy link to clipboard

Copied

Hi,

You can do this by using the "anchors" object in "Javascript".. Below is the Sample Code..


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns=" http://www.w3.org/1999/xhtml">
<head>
<script language="javascript">
function fnCheckAnchors()
{
for (var i=0;i<document.anchors.length;i++)
{
alert(document.anchors.item(i));
}
}
</script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<form name="form" action="" method="post" onsubmit="fnCheckAnchors();">
<a name="1" href=" http://www.yahoo.com">Yahoo</a><br />
<a name="2" href=" http://www.google.com">Google</a><br />
<a name="3" href=" http://www.altavista.com">Altavista</a><br />
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Apr 19, 2007 Apr 19, 2007

Copy link to clipboard

Copied

Thanks for taking the time to reply - much appreciated. Unfortunately this isn't what I need.

I probably wasn't clear enough in the original post, allow me to ellaborate:
These data strings will be coming from a database.
Basically the URL's that i want to extract are old and need to be replaced with new URLS... because the links have been inserted using dynamic content, I can't just do a simple replace() because I don't / wont know what the links are... I need something that will detect where the "<a href=" is and be able to determine how long that href value is and put that into a variable.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 19, 2007 Apr 19, 2007

Copy link to clipboard

Copied

LATEST
regular expressions will help you do what you want
--

Azadi Saryev
Sabai-dee.com
http://www.sabai-dee.com

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation