Hi I have some vars like this:
var a = Base-Shirt_Stripe.jpg
var b = Closed-Flatknit-Collar_Stripe.png
How do i create two new vars like:
var c = Base-Shirt
var d = Stripe
or
var e = Closed-Flatknit-Collar
var f = Stripe
basically split at the _
remove the _
and remove the extension.
//for example, we take a
var a = 'Base-Shirt_Stripe.jpg';
//then we take the part of a before the dot
//and split between `_`
//split returns an array
var split = a.substring(0,a.indexOf('.'))
.split('_');
//split is an array, so we use indices to indicate which
console.log(split[0]); //Base-Shirt
console.log(split[1]); //Stripe
Sample here . You can do the same for your b
You need to make them strings to start with, then use String.split()
to split the string into an array of the different parts.
var a = "Base-Shirt_Stripe.jpg"
var b = "Closed-Flatknit-Collar_Stripe.png"
var aSplit = a.substr(0, a.lastIndexOf('.')).split('_');
var c = aSplit[0];
var d = aSplit[1];
var bSplit = b.substr(0, b.lastIndexOf('.')).split('_');
e = bSplit[0];
f = bSplit[1];
You could also take the removal of the extension out into its own function using String.lastIndexOf()
and String.substr()
.
function removeExtension(file) {
return file.substr(0, file.lastIndexOf('.'));
}
may be you could do like
var a = "Base-Shirt_Stripe.jpg"
var k = a.replace(/(\.\w*)$/g, "").split("_");
alert(k[0]);
alert(k[1]);
here is the fiddle
//Javascript Split can divide it into parts. Javascript Split return type is array.'
//e g.
var a = Base-Shirt_Stripe.jpg
var parts = a.split('_');
console.log(parts[0]);
//output
" Base-Shirt "
//parts[0] contain base-shirt and parts[1] contain Stripe.jpg.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.