当前位置:网站首页>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
边栏推荐
- Socket programming server and client (multiple clients can connect to the same port of a server at the same time)
- 动态链接库嵌套样例
- Overview of demoplayer program framework design of ijkplayer
- Differences among concurrent, parallel, serial, synchronous and asynchronous
- MFS explanation (V) -- MFS metadata log server installation and configuration
- JetPack - - - DataBinding
- ADB shell content command debug database
- Scrcpy development environment construction and source code reading
- 线程池学习
- 347. top k high frequency elements heap sort + bucket sort +map
猜你喜欢

The processing and application of C language to documents

RN Metro packaging process and sentry code monitoring

Kotlin basic definition class, initialization and inheritance

Uniapp secondary encapsulates uview components, and the parent component controls display and hiding

‘ipconfig‘ 不是内部或外部命令,也不是可运行的程序 或批处理文件。

SSM framework integration -- > simple background management

【2022高考季】作为一个过来人想说的话

JetPack - - - LifeCycle、ViewModel、LiveData

MFS details (vii) - - MFS client and Web Monitoring installation configuration

如何使用望友DFM软件进行冷板分析
随机推荐
c语言对文件相关的处理和应用
If the key in redis data is in Chinese
Detailed explanation of the player startup process of ijkplayer code walkthrough 2
[case] a super easy-to-use tool -- a calculator for programmers
景联文科技:数据采集标注行业现状及解决方案
Kotlin collaboration - start and cancel, scope
RFID process management solution for electroplating fixture
時間格式化工具----moment.js(網頁時間實時展示)
El form form verification
Detailed explanation of scrcpy client code walk through H264 raw stream decoding process
Wechat applet development (requesting background data and encapsulating request function)
Ijkplayer compilation process record
数据在内存中的存储(C语言)
‘ipconfig‘ 不是内部或外部命令,也不是可运行的程序 或批处理文件。
JetPack - - - LifeCycle、ViewModel、LiveData
Win10 drqa installation
Usegeneratedkeys=true configuration
C # mapping from entity class to database (SQLite)
线程池学习
Pngquant batch bat and parameter description