Javascript and reg exp

SpaceshipSpaceship
SpaceshipSpaceship
SpaceshipSpaceship
Watch

Wuoshi

Established Member
Impact
8
I got a Javascript which is receiving a url, for example product1_product2_product3_.html. When the url is received I want to split it in to three variables, var1 which is product1, var2 which is product2 and so on.

I figure this can be solved by using regular expressions? Anybody know how?

Thanks in advance for any answers.
 
0
Close
The views expressed on this page by users and staff are their own, not those of NamePros.
GoDaddyGoDaddy
you don't have to use regular expressions, if it's always split by a _, it would be much easier to use split. something like this:

Code:
var str = "product1_product2_product3_.html";
str = str.substr(0, -5);
split = str.split("_");

now product1 is stored in split[0], product2 is in split[1], product3 is in split[2], etc.

however, note that the negative string length for substr doesn't work in IE (as far as i know), so you might want to consider trying to switch this to PHP.
 
1
Close
Ahh..great! Will look into it.

Thank you!
 
0
Close
Appraise.net

We're social

Escrow.com
Spaceship
Domain Recover
CryptoExchange.com

NamePros updates

Switch between accounts
You can now stay logged in to more than one NamePros account in the same browser. Click your avatar, then "Accounts in this browser", to...
AIVikings
Catchy
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back