在 JavaScript 中從字符串中去除所有非數字字符 - Strip all non-numeric characters from string in JavaScript

問題:

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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章