Reputation: 2093
i have some data ab-cde0001
. i want to separate it become two part: ab-cde
and 0001
.
i'm just know to separate data if consist of some space inside it.
how do i do that?
var barcode;
barCode=$("#model").val();
var data=barCode.split(" "); //must change this
$("#model").val(data[0]);
$("#serial").val(data[1]);
i have another idea, but i dont know how to start.
may be i'm just need some script read 4 digits behind the text, then split the choosen one.
Upvotes: 0
Views: 1798
Reputation: 2093
i have combined the answer before, and then i found the real answer:
var barcode;
barCode=$("#model").val();
var data1 = $("#model").val(barCode.substr(0,barCode.length-4));
var data2 = $("#serial").val(barCode.substr(barCode.length-4,4));
Upvotes: 0
Reputation: 29427
Try this code:
var barcode;
barCode = $("#model").val();
$("#model").val( barCode.substring(0, 6) );
$("#serial").val( str.substring(6, 4) );
Upvotes: 0
Reputation: 2697
If you always just want to chop off the last 4 digits, it's easy. Presuming your string variable is s:
part1 = s.substr(0, s.length-4);
part2 = s.substr(s.length-4, 4);
Upvotes: 0
Reputation: 2697
Try the following. s needs to be your string to split. The code splits it so that all numbers from the end will be in t, and everything before the numbers will be in s. Doesn't matter how many digits.
var s = 'ab-cde0001';
var t = '';
while ('0123456789'.indexOf(s.substr(s.length-1, 1))!=-1) {
t = s.substr(s.length-1, 1)+t;
s = s.substr(0, s.length-1);
}
Upvotes: 0
Reputation: 490183
Someone had to provide a regex solution...
var str = 'ab-cde0001';
var matches = str.match(/^([a-z]{2}-[a-z]{3})(\d{4})$/);
matches[1]
will have the first part, and matches[2]
will have the second part.
Upvotes: 2
Reputation: 70701
If you always have 6 characters and 4 digits, you can just use the substring
method to get the two parts:
var str = 'ab-cde0001';
var part1 = str.substring(0, 6),
part2 = str.substring(6, 10);
Upvotes: 0