当前位置:网站首页>JS method of extracting numbers from strings
JS method of extracting numbers from strings
2022-06-13 06:39:00 【A-linWeb】
1 parseInt() Method :
var str ="4500 element ";
var num = parseInt(str);
alert(num);//4500
The result is what we want , Thought it was so simple , It would be wrong . If the string is preceded by a non numeric character , The above method will not work :
var str =" Price :4500 element ";
var num = parseInt(str);
alert(num);//NaN
This example will pop up NaN, Solve this problem , The easiest way is : If you know the string format , Remove the preceding non characters . In the example above , Remove substring " Price :"
var str =" Price :4500 element ";
var num = parseInt(str.substring(1).substring(1).substring(1));
alert(num);//4500
obviously , This is more troublesome .Js Medium parseInt() Method can pass a non numeric string , As long as the string is preceded, it runs the same , Know to stop when you encounter non numeric characters . such as , Here's an example .
var str ="4500 element , Grade :2";
var num = parseInt(str);
alert(num);//4500
2. Regular
in fact , Regular processing is relatively simple , Just replace non numeric characters . Example :
var s =" Price 4500 element ";
var num= s.replace(/[^0-9]/ig,"");
alert(num);//4500
If you encounter characters with numbers :
var s =" Price 4500 element , Grade :2";
var num = s.replace(/[^0-9]/ig,"");
alert(num);//45002
边栏推荐
- JetPack - - - DataBinding
- [case] a super easy-to-use tool -- a calculator for programmers
- Explication détaillée du triangle Yang hui
- Ijkplayer code walkthrough player network video data reading process details
- 景联文科技提供语音数据采集标注服务
- [JS] array flattening
- Cross process two-way communication using messenger
- Dragon Boat Festival wellbeing, use blessing words to generate word cloud
- SSM integration
- 时间格式化工具----moment.js(网页时间实时展示)
猜你喜欢

数据在内存中的存储(C语言)

MFS explanation (V) -- MFS metadata log server installation and configuration

机器学习笔记 - 监督学习备忘清单

Analyzing server problems using jvisualvm

智能文娱稳步发展,景联文科技提供数据采集标注服务

Pngquant batch bat and parameter description

Relationship between fragment lifecycle and activity

El form form verification

景联文科技提供语音数据采集标注服务

Solutions to common problems in small program development
随机推荐
App performance test: (II) CPU
Analyzing server problems using jvisualvm
JetPack - - - Navigation
Explication détaillée du triangle Yang hui
[FAQs for novices on the road] understand program design step by step
Kotlin basic objects, classes and interfaces
Kotlin basic string operation, numeric type conversion and standard library functions
Recyclerview has data flicker problem using databinding
Detailed explanation of the player startup process of ijkplayer code walk through
Kotlin collaboration - flow+room database
Notes on wechat applet development
【sketchup 2021】草图大师中CAD文件的导入与建模(利用cad图纸在草图大师中建立立面模型)、草图大师导出成品为dwg格式的二维、三维、立面效果到cad中打开预览】
DataGridView data export to excel (in case of small amount of data)
Detailed explanation of the player startup process of ijkplayer code walkthrough 2
Custom view subtotal
synchronized浅析
Array operations in JS
无刷直流电机矢量控制(四):基于滑模观测器的无传感器控制
ADB shell CMD overlay debugging command facilitates viewing system framework character resource values
Learning records countless questions (JS)