?
Solved

javascript closest match wildcard

Posted on 2011-02-25
6
Medium Priority
?
408 Views
Last Modified: 2012-05-11
i have a script that looks up an image name based sting name

but i would like it to be able to make a close match using a wildcard

document.getElementById('resultImg').src='flags/'+callCost[0]+'.png';

how can i set callcost[0] to be a closest match to and image in a folder containing *.png images

callcost list

Afghanistan-Kabul
Afghanistan-Mobile
Afghanistan-Mobile-Areeba
Afghanistan-Mobile-Etisalat
Afghanistan-Mobile-Roshan

folder images
Afghanistan.png

so the callcost selection matches the folder image name image
0
Comment
Question by:daz1234
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 16

Expert Comment

by:sjklein42
ID: 34985326
I am trying to understand.

You want to find the first part of the string, everything up to the first "-" dash character:

Afghanistan-Kabul
Afghanistan-Mobile
Afghanistan-Mobile-Areeba
Afghanistan-Mobile-Etisalat
Afghanistan-Mobile-Roshan

and then you want to use that Afghanistan string to load an image called Afghanistan.png?

Is it like this:

France-Paris would go to France.png
England-London would go to England.png
England-London-Heathrow would also go to England.png
England (all by itself) would also go to England.png


Do I understand the question?
0
 

Author Comment

by:daz1234
ID: 34986178
Yes that it
0
 
LVL 16

Expert Comment

by:sjklein42
ID: 34986817
Perhaps this will help.

Here is a function called GetImageName that will return the "base" image name given a string that may have dashes in it.

All five examples below return "Afghanistan.png".

<script>
function GetImageName(s)
{
	var dashPos = s.indexOf('-');
	if ( dashPos == (-1) ) return s+'.png';
	return s.substring(0, dashPos)+'.png';
}


alert(GetImageName('Afghanistan-Kabul'));
alert(GetImageName('Afghanistan-Mobile'));
alert(GetImageName('Afghanistan-Mobile-Areeba'));
alert(GetImageName('Afghanistan-Mobile-Etisalat'));
alert(GetImageName('Afghanistan'));

</script>

Open in new window

0
WordPress Tutorial 1: Installation & Setup

WordPress is a very popular option for running your web site and can be used to get your content online quickly for the world to see. This guide will walk you through installing the WordPress server software and the initial setup process.

 

Author Comment

by:daz1234
ID: 34987382
this is the existing script i have how would i integrate your function into it

<script language="JavaScript" type="text/javascript">
                              <!--
                              function callRef() {
                                    // COUNTRY SELECTOR
                                    if (document.selector1.select1.options[document.selector1.select1.selectedIndex].value != "0") {
                                          var callCosts = document.selector1.select1.options[document.selector1.select1.selectedIndex].value;
                                          callCost = callCosts.split('?');
                                          //= this is were i want enter your function=> document.getElementById('resultImg').src='flags/90x90/'+callCost[0]+'.png';
                                          document.getElementById('rate1').innerHTML=''+callCost[1]+' &#36;/min';
                                          document.getElementById('rate2').innerHTML=''+callCost[2]+' &#36;/min';      
                                    }
                              }
                              //-->
                        </script>
0
 
LVL 16

Accepted Solution

by:
sjklein42 earned 2000 total points
ID: 34987871
I think this is what ou're looking for
<script language="JavaScript" type="text/javascript">
<!--
function GetImageName(s) {
    var dashPos = s.indexOf('-');
    if ( dashPos == (-1) ) return s;
    return s.substring(0, dashPos);
}

function callRef() {
    // COUNTRY SELECTOR
    if (document.selector1.select1.options[document.selector1.select1.selectedIndex].value != "0") {
        var callCosts = document.selector1.select1.options[document.selector1.select1.selectedIndex].value;
        callCost = callCosts.split('?');

        //= this is were i want enter your function=> document.getElementById('resultImg').src='flags/90x90/'+callCost[0]+'.png'; 

        document.getElementById('resultImg').src= 'flags/90x90/' + GetImageName(callCost[0]) + '.png'; 

        document.getElementById('rate1').innerHTML=''+callCost[1]+' &#36;/min';
        document.getElementById('rate2').innerHTML=''+callCost[2]+' &#36;/min';      
    }
}
//-->
</script> 

Open in new window

0
 

Author Closing Comment

by:daz1234
ID: 34999065
worked like a treat thanks
0

Featured Post

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Finding original email is quite difficult due to their duplicates. From this article, you will come to know why multiple duplicates of same emails appear and how to delete duplicate emails from Outlook securely and instantly while vital emails remai…
Originally, this post was published on Monitis Blog, you can check it here . Websites are getting bigger and more complicated by the day. Video, images and custom fonts are all great for showcasing your product or service. But the price to pay in…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question