Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove suffix from string?

Tags:

javascript

How can I remove the following suffix:

  1. px
  2. %
  3. 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.

like image 208
Damir Avatar asked Oct 20 '25 05:10

Damir


1 Answers

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
like image 135
Fabrizio Calderan Avatar answered Oct 21 '25 17:10

Fabrizio Calderan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!