For example, there is a string a = "8px";
The number of digits in the number is not necessarily certain. If I want to intercept the number, I want to use a.substring(0,a.indexOf("p")). I originally wanted to use a.substring(0,-2); But after checking, I found out that the substring parameter cannot be a negative number, but I think a.substring(0,a.indexOf("p")) is a bit troublesome. Is there a more direct optimization method?
var a="88px";
If the format is the same, the first parts are numbers and only the numbers need to be extracted, you can use:
parseInt(a);//88
Use regular rules
The first one can use the substring method: a.substring(0,a.length-2)
The second one can use regular expressions: var a='8px';a.replace(/px$/ig,'' )
parseInt,parseFloat,slice,regexp
It is most convenient to use regular expressions
I’m speechless for you guys, can’t I use parseInt, parseFloat