How can I remove the following suffix:
- px
- %
- em
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.
How can I remove the following suffix:
- px
- %
- em
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.
- You actually need to get the numeric part... – Itay Moav -Malimovka Commented May 7, 2012 at 12:24
- 1 @Damir, if this problem was solved consider to accept an answer – Fabrizio Calderan Commented Oct 6, 2012 at 12:59
2 Answers
Reset to default 7var 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
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);