I’m trying to change a string to the sign, numbers and decimal only.
The following only changes the strings to numbers only.
I want to be able to use the numbers in a CSS assignment, like width:200px, without the px if present.
Here are my attempts. Any recommended modifications?
[code]
<!DOCTYPE html>
<html lang=”en”>
<head>
<meta charset=”UTF-8″ />
<title> HTML5 page </title>
</head>
<body>
<script type=”text/javascript”>
// Want to leave only sign (-) and/or number (0-9) and/or decimal (.) leaving only signed (optional) integers and decimals.
// String.prototype.toNum = function() { var n = this.replace(/D/g, ”); return n; } // leaves #s only
String.prototype.toNum = function() { var n = this.replace(/D|-|./g, ”); return n; } // leaves #s only too (drops – and .)
alert(’10px’.toNum()+’nn’+’-10px’.toNum()+’nn’+’11.5px’.toNum());
</script>
</body>
</html>