問題:
Consider a non-DOM scenario where you'd want to remove all non-numeric characters from a string using JavaScript/ECMAScript.考慮一個非 DOM 場景,您希望使用 JavaScript/ECMAScript 從字符串中刪除所有非數字字符。 Any characters that are in range 0 - 9
should be kept.應保留0 - 9
範圍內的任何字符。
var myString = 'abc123.8<blah>';
//desired output is 1238
How would you achieve this in plain JavaScript?你將如何在純 JavaScript 中實現這一點? Please remember this is a non-DOM scenario, so jQuery and other solutions involving browser and keypress events aren't suitable.請記住,這是一個非 DOM 場景,因此 jQuery 和其他涉及瀏覽器和按鍵事件的解決方案不適合。
解決方案:
參考一: https://en.stackoom.com/question/7oQM參考二: https://stackoom.com/question/7oQM