473,398 Members | 2,113 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,398 software developers and data experts.

Extracting the img src using JavaScript RegExp

Hello all - I'm a newbie here with a question.

I work with a WCMS and what I am attempting to do is extract the image source from a HTML hyperlink/image.

In a nutshell, I have created an array which takes a text object (the WCMS calls this a WEM tag). I want to preload the image embedded within that text object as the thing I am creating is an image rotator. It looks something like this...

Expand|Select|Wrap|Line Numbers
  1.   if (document.images) {  
  2.     img1 = new Image();
  3.     //...
  4.     img1.src = <WEM TEXT0005></WEM>;
  5.     //...
  6.   }
  7.  
  8.   var tileArray = new Array();
  9.   tileArray[0] = <WEM TEXT0005></WEM>
  10.   //...
  11.  
I am trying to achieve this outside of the WCM to begin with and once that works I can plug in the code.

What I need returned is something like...

"advert-tile-1.jpg"

At the moment I have...

Expand|Select|Wrap|Line Numbers
  1. <script language="JavaScript"> 
  2.  
  3.   reg = /src=\".+\.jpg\"/; 
  4.   string = new String ('<a href="blah.htm" title="Blah"><img src="advert-tile-1.jpg" width="175" height="180" alt="" border="0" /></a>');
  5.  
  6.   var matches = string.match(reg);
  7.  
  8.   document.write(matches);
  9.  
  10. </script>
  11.  
This returns:

src="advert-tile-1.jpg"

So I just need to get rid of the 'src='.

In any other language you could use a Positive Lookbehind (?<=text) on the 'src=' but seeing as JavaScript does not support this, I'm a little stumped!

Any help would be greatly appreciated.

Thank you.
Sep 7 '07 #1
3 16738
gits
5,390 Expert Mod 4TB
heya ...

welcome to TSDN ...

have a look at the following example:

Expand|Select|Wrap|Line Numbers
  1. //something like your string :)
  2. var src_attrib = 'src="test.jpg"';
  3.  
  4. // we match from the end and use the brackets that
  5. // extract the match from the string - you get an array
  6. // returned ... so we use item 1 that is our match
  7. // and we replace the " with an empty string 
  8. var img_src = src_attrib.match(/src=(.+)$/)[1].replace(/\"/g,'');
  9.  
may be it helps :)

kind regards
Sep 7 '07 #2
Hi Gits,

Thank you so much for your help with this. I actually adapted your solution to the following...

Expand|Select|Wrap|Line Numbers
  1. <script language="JavaScript">
  2.  
  3.   var img_src1 = new String('<a href="blah.htm" title="Blah"><img src="advert-tile-1.jpg" width="175" height="180" alt="" border="0" /></a>').match(/src=(.+?[\.jpg|\.gif]")/)[1];
  4.  
  5.   document.write(img_src1);
  6.  
  7. </script>
  8.  
Which returns...

"advert-tile-1.jpg"

Now all I've got to do is make it work in this blasted WCMS, which is a lot easier said than done!
Sep 10 '07 #3
gits
5,390 Expert Mod 4TB
hi ...

glad to hear you got it working ... post back to the forum anytime you have more questions :)

kind regards
Sep 10 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Mark | last post by:
I am creating a search engine that will scan pages on my Web site. I do not want any false-positive hits, being defined as any match that does not appear on the page using a Web browser. I am...
1
by: Cognizance | last post by:
Hi gang, I'm an ASP developer by trade, but I've had to create client side scripts with JavaScript many times in the past. Simple things, like validating form elements and such. Now I've been...
2
by: jatinder.singh | last post by:
Hi All, I am trying to Format a given TSQL code so that it is well indented and easy to understand It is moderately working with simple scripts, but where the script involve some part of...
0
by: Bubbles | last post by:
Hello. New to ASP.NET and struggling on this one. I have a text file with a bunch of text in it. Throughout the file words followed by a ":" will appear. I need to pull every such string out...
14
by: Adnan Siddiqi | last post by:
Hi Suppose I have following URLs comming from an HTML document <a href="http://mydomain1.com">Domain1</a> <a...
1
by: Phat G5 (G3) | last post by:
I found this little script for extracting parameters from a url but wondered what the shortest and most efficient way to do it would be, like the following or via regexp? function...
16
by: shyamg | last post by:
Hi, this is my javascript validating the fields in mozilla FF but its working and validating only one field. how to write the and how to works the script .................. function...
4
by: Velhari | last post by:
Hi all, I wrote the following Javascript function used to execute the Javascript codes from the ajax response which contains both html & javascript. It works fine, if the javascript codes from...
2
by: joelkeepup | last post by:
Hi, I made a change this morning and now im getting an error that says either "a is undefined or null" or "e is undefined or null" the microsoft ajax line is below, I have no idea how to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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
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
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...
0
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
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...

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.