SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Hybrid View

  1. #1
    SitePoint Wizard westmich's Avatar
    Join Date
    Mar 2000
    Location
    Muskegon, MI
    Posts
    2,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help with Regular Expressions in JavaScript

    I am trying to use regular expressions within JavaScript for the picture upload component of a shopping cart. I still can't seem to get my mind around them.

    I have a page with a working file upload. When you click on browse and then select your file, the file name is also returned to the second field, the picture name field. The problem is that the entire string is returned - 'G:\Catalog\MyPictures\somepic.jpg' instead of 'somepic.jpg'.

    I know this can be pulled out with regular expressions, everything from the right until it hits a / or \ I included the script and a couple of links.

    working example http://www.westmichiganwebservices.com/conn.asp

    reg exp javascript tutorial http://www.webreference.com/js/column5/define.html

    working javascript code thus far -
    Code:
    <script language="JavaScript" type="text/javascript">
    function addname() {
     var newfile = document.form1.upload_field.value;
     //newfile = the result of some regular expression;
     document.form1.filename.value = newfile;
     return false;
    }
    </script>
    Westmich
    Smart Web Solutions for Smart Clients
    http://www.mindscapecreative.com

  2. #2
    JavaScript Guru (Big Ego) Arielladog's Avatar
    Join Date
    Jul 1999
    Location
    SC, USA
    Posts
    390
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey,

    I'm definately not an expert with regular expressions and not fond of them. I usually can get by without using them, and in this situation, you can too. Here's some code I created. I had to escape the "\" character, but I'm not sure how it'll work with you..whether JS will automatically escape it or not:

    x="G:\\Catalog\\MyPictures\\somepic.jpg";
    x=x.substring(x.lastIndexOf("\\")+1);

    aDog

  3. #3
    SitePoint Wizard westmich's Avatar
    Join Date
    Mar 2000
    Location
    Muskegon, MI
    Posts
    2,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Not sure what you're doing there, Arielladog.
    Westmich
    Smart Web Solutions for Smart Clients
    http://www.mindscapecreative.com

  4. #4
    SitePoint Wizard westmich's Avatar
    Join Date
    Mar 2000
    Location
    Muskegon, MI
    Posts
    2,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oooooh! Now I get it!
    Code:
    <script language="JavaScript" type="text/javascript">
    function addname() {
     var newfile = document.form1.upload_field.value;
    
     //newfile = the result of some regular expression;
     newfile=newfile.substring(newfile.lastIndexOf("\\")+1);
    
     document.form1.filename.value = newfile;
     return false;
    }
    </script>
    Westmich
    Smart Web Solutions for Smart Clients
    http://www.mindscapecreative.com


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •