Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Jan 2009
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Get dimensions from text

    I have a string that looks similar to this:
    Code:
    tumblr_mgn6mfe9aD1rbaz5bo1_500.jpg (500×705)
    I want to strip the text until I get the value 500 & 705. I want them as two separate values. How can I do this in javascript?

  • #2
    Regular Coder
    Join Date
    Mar 2008
    Location
    London
    Posts
    152
    Thanks
    4
    Thanked 42 Times in 42 Posts
    Code:
    var str = 'tumblr_mgn6mfe9aD1rbaz5bo1_500.jpg (500x705)';
    var index1 = str.indexOf('('), index2 = str.indexOf(')');
    str = str.substr(index1+1, (index2-index1-1));
    str = str.split('x');
    alert(str);
    //str[0] = 500
    //str[1] = 705

  • #3
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,296
    Thanks
    10
    Thanked 584 Times in 565 Posts
    i find split simpler, but there's probably a thousand ways to do this:
    Code:
    "tumblr_mgn6mfe9aD1rbaz5bo1_500.jpg (500×705)"
       .split(")")[0].split("(")[1].split("x").map(Number);
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • #4
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    Allows for x or the character you used between the numbers: × (Hex FFFD)

    Code:
    var str = "tumblr_mgn6mfe9aD1rbaz5bo1_500.jpg (500x705)",
        dims = str.match( /\((\d+)(?:\uFFFD|x)(\d+)\)/ ),
        width = 0, 
        height = 0;
    
    if( dims )
    {
      width = dims[ 1 ];
      height = dims[ 2 ];
      
      alert("Width=" + width + ", height=" + height );
    }


  •  

    Posting Permissions

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