简体   繁体   中英

To remove the alphabets and special characters from the floating number?

I have below string in that i have to filter out alphabets and special characters and also i have to remove the dot which is appearing after the decimal point.

var number = '1.25eretr6565....$#$%'
number.replace( /[^0-9.]/g, '' );

For the above am getting the result as "1.256565...."

but the expected result is 1.256565 . Have to remove the dot which is appearing after the decimal point?

You can try this:

number.replace(/[^0-9.]/g, '' ).split(".").filter(item => item !== "").map((item, index) => (index === 0)?item+'.' : item
).join('')

Happy to help

You Can try this:

function getNumberValueFromAlphanumeric(number) {
  var numberValue = number.replace( /[^0-9.]/g, '' );
  var valueArray = numberValue.split(".").filter(item => item !== "");
  // extract first value 
  var firstValue = valueArray[0];
  //remove first value from array
  valueArray.shift()
  //create second value
  var secondValue = valueArray.join("");
  var expectedValue = firstValue + "." + secondValue;
  return expectedValue;
}

this works for all value that I can think of at this moment Happy to help

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM