473,805 Members | 1,882 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Extracting the img src using JavaScript RegExp

3 New Member
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 16784
gits
5,390 Recognized Expert Moderator Expert
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
Ollie Hoad
3 New Member
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 Recognized Expert Moderator Expert
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
10546
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 using a string of regexp statements to do this. The first one removes anything before the <body> tag since it contains, among other things, general keywords and function definitions. The goal of the next one is to remove <script including...
1
2808
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 assigned the task of extracting content from a given HTML page. If anyone's familiar with the Yahoo! Store order confirmation screen, I need to be able to grab the total amount from the table to the right-hand side. (Sample File:
2
1715
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 keywords (like endrosmentnumber has end in it)it fails Please guide me with the regular Expression stuff so that I can make it work.
0
1201
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 and then display all unique values. I am able to read the file in, but am having trouble getting regex to work. What regex and setting should I use?
14
3772
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 href="http://subdomain.domain.com/myfile.anyext">http://subdomain.domain.com/myfile.anyext</a> <a href="http://subdomain.domain2.com/myfile.anyext">Domain2</a>
1
1601
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 getParameter(paramName) { var currentUrl = window.location.search var strBegin = currentUrl.indexOf(paramName) + (paramName.length+1) var strEnd = currentUrl.indexOf("&",strBegin) if (strEnd==-1)
16
1967
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 Test(){
4
2599
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 the response doesn't have any line break at all. function executeJSCodes( html ){ var regExp = new RegExp('<script language="javascript">(.*)<\/script>',"gi"); while( res = regExp.exec(html) ){ eval(res); ...
2
9044
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 figure this problem out. Any suggestions? thanks Joel
0
9596
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10105
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
9185
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...
1
7646
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6876
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5542
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
4323
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
3845
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3007
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.