Dynadot โ€” .com Transfer

Javascript and reg exp

Spacemail by SpaceshipSpacemail by Spaceship
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
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
Unstoppable DomainsUnstoppable Domains
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
•••
Ahh..great! Will look into it.

Thank you!
 
0
•••
Appraise.net
Domain Recover
DomainEasy โ€” Live Options
  • The sidebar remains visible by scrolling at a speed relative to the pageโ€™s height.
Back