Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-15-2013, 01:27 PM   PM User | #1
Ruriko
New Coder

 
Join Date: Jan 2009
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Ruriko is an unknown quantity at this point
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?
Ruriko is offline   Reply With Quote
Old 01-15-2013, 02:26 PM   PM User | #2
niralsoni
Regular Coder

 
Join Date: Mar 2008
Location: London
Posts: 129
Thanks: 1
Thanked 31 Times in 31 Posts
niralsoni is an unknown quantity at this point
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
niralsoni is offline   Reply With Quote
Old 01-15-2013, 02:41 PM   PM User | #3
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,452
Thanks: 9
Thanked 466 Times in 450 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
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 5/13)
STATS (2013/5) HTML5:90.2% MOB:14% IE7:0.5% IE8:8.8% IE9:11.4% IE10:6.5%
rnd me is offline   Reply With Quote
Old 01-15-2013, 02:45 PM   PM User | #4
Logic Ali
Regular Coder

 
Logic Ali's Avatar
 
Join Date: Sep 2010
Location: London
Posts: 959
Thanks: 0
Thanked 198 Times in 193 Posts
Logic Ali will become famous soon enoughLogic Ali will become famous soon enough
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 );
}
Logic Ali is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:29 PM.


Advertisement
Log in to turn off these ads.