I am very new at jquery...how would I split a strings like this:
readybuilt-ANT-133.pdf
readybuilt-VIC-041.pdf
so I get just the numbers 133 and 41
this what I tried thus far
var str = this.home_pdf;
var res = str.split("-");
var lotNumber = res[2].split(".");
lotNumber = lotNumber[0];
console.log(lotNumber);
this works well, but how would I remove the leading 0s?
Try
parseInt
( lotNumber, 10 )
.
It will not care about leading zeroes when given an appropiate radix parameter.
PS: obviously, regex is the way to go here.
A much better way is to use Bergi's method
var str = "readybuilt-VIC-041.pdf";
var lotNumber = str.split(/\.|-0*/)[2];
console.log( lotNumber );
Parse the variable as an integer:
lotNumber = lotNumber[0];
lotNumber = parseInt(lotNumber,10);
console.log(lotNumber);
assuming that this.home_pdf
contains the string, this should do the trick:
var str = this.home_pdf;
str=parseInt(str.replace(/\D/g,''));
if(str.match(/^0/g)){
str=parseInt(str.replace(/0/,''));
}
Yet another solution, using match()
'readybuilt-ANT-133.pdf'.match(/(?=[1-9])([0-9]+)/g); // 133
'readybuilt-VIC-041.pdf'.match(/(?=[1-9])([0-9]+)/g); // 41
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.