Reputation: 56199
How can I remove the following suffix:
from some string if it contains that suffix ?
Like enter I get string which can be measure of width of div and that can ends with 'px', '%', 'em' and can also be without, so I need to remove suffix if it exists.
Upvotes: 4
Views: 4352
Reputation: 123397
var s = "34em";
parseInt(s, 10); // returns 34
this works for em, px, %, pt
... and any other suffix even if it has a space before.
Use parseFloat()
instead if you have non-integer values
var s = "81.56%";
parseFloat(s); // returns 81.56
Upvotes: 7
Reputation: 700412
You can use a regular expression to get a string with only the digits:
var s = input.replace(/\D+/g, '');
You can parse the string as a number, that will ignore the non-numeric characters at the end:
var n = parseInt(input, 10);
Upvotes: -1