473,395 Members | 2,468 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

get number from string

If I have a string with numerals in it and I want to get each number in
sequence. Is there a way to do it without parsing the string character
by character? For example,

str = "abc13def345gh56.7fghdfgf"
/*
get number(1) returns 13
get number(2) returns 345
get number(3) returns 56.7
get number(4) returns undefined
*/

I thought of maybe a regex that does a split on the string but I'm not
any good with regex and the resulting array will have string elements
instead of numbers.

Andrew Poulos
Aug 28 '06 #1
6 22073
Andrew Poulos wrote on 28 aug 2006 in comp.lang.javascript:
If I have a string with numerals in it and I want to get each number in
sequence. Is there a way to do it without parsing the string character
by character? For example,

str = "abc13def345gh56.7fghdfgf"
/*
get number(1) returns 13
get number(2) returns 345
get number(3) returns 56.7
get number(4) returns undefined
*/
<script type='text/javascript'>

var s = 'abc13def345gh56.7fghdfgf';
var r = s.match(/[\d\.]+/g);

alert(r[0])
alert(r[1])
alert(r[2])
alert(r[3])

</script>

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Aug 28 '06 #2
Andrew Poulos wrote:
If I have a string with numerals in it and I want to get each number in
sequence. Is there a way to do it without parsing the string character
by character? For example,

str = "abc13def345gh56.7fghdfgf"
/*
get number(1) returns 13
get number(2) returns 345
get number(3) returns 56.7
get number(4) returns undefined
*/]
'abc13def345gh56.7fghdfgf'.match(/\d+\.?\d*/g);
== ["13","345","56.7"]
>
I thought of maybe a regex that does a split on the string but I'm not
any good with regex and the resulting array will have string elements
instead of numbers.
it's not hard to master regex.
Step 1. master regex itself
Step 2. learn 5 methods: match, exec, search, test, split
That's all.
(BTW I still can't understand why the regex
(aa|ab(bb)*ba)*(b|ab(bb)*a)(a(bb)*a|(b|a(bb)*ba)(a a|ab(bb)*ba)*(b|ab(bb)*a))*
denotes the set of all strings which contain an even number of as and
an odd number of bs.)

To convert string elements to numbers, consider parseInt.

Aug 28 '06 #3
Evertjan. wrote:
Andrew Poulos wrote on 28 aug 2006 in comp.lang.javascript:
>If I have a string with numerals in it and I want to get each number in
sequence. Is there a way to do it without parsing the string character
by character? For example,

str = "abc13def345gh56.7fghdfgf"
/*
get number(1) returns 13
get number(2) returns 345
get number(3) returns 56.7
get number(4) returns undefined
*/

<script type='text/javascript'>

var s = 'abc13def345gh56.7fghdfgf';
var r = s.match(/[\d\.]+/g);

alert(r[0])
alert(r[1])
alert(r[2])
alert(r[3])

</script>
Thanks a simple multiplication by 1 and I've got my numbers.

Andrew Poulos
Aug 28 '06 #4
Andrew Poulos said the following on 8/28/2006 6:51 AM:
Evertjan. wrote:
>Andrew Poulos wrote on 28 aug 2006 in comp.lang.javascript:
>>If I have a string with numerals in it and I want to get each number
in sequence. Is there a way to do it without parsing the string
character by character? For example,

str = "abc13def345gh56.7fghdfgf"
/*
get number(1) returns 13
get number(2) returns 345
get number(3) returns 56.7
get number(4) returns undefined
*/

<script type='text/javascript'>

var s = 'abc13def345gh56.7fghdfgf';
var r = s.match(/[\d\.]+/g);

alert(r[0])
alert(r[1])
alert(r[2])
alert(r[3])

</script>
Thanks a simple multiplication by 1 and I've got my numbers.
alert(typeof +r[0])

unary + is known to be faster than *1.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Aug 28 '06 #5
Randy Webb wrote on 28 aug 2006 in comp.lang.javascript:
>><script type='text/javascript'>

var s = 'abc13def345gh56.7fghdfgf';
var r = s.match(/[\d\.]+/g);

alert(r[0])
alert(r[1])
alert(r[2])
alert(r[3])

</script>
Thanks a simple multiplication by 1 and I've got my numbers.

alert(typeof +r[0])
alert(r[3]) // undefined
alert(+r[3]) // NaN
alert(typeof r[3]) // undefined
alert(typeof +r[3]) // number
unary + is known to be faster than *1.
Indeed.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Aug 28 '06 #6
JRS: In article <44f2b861$0$10787$5a62ac22@per-qv1-newsreader-
01.iinet.net.au>, dated Mon, 28 Aug 2006 19:33:38 remote, seen in
news:comp.lang.javascript, Andrew Poulos <ap*****@hotmail.composted :
>
I thought of maybe a regex that does a split on the string but I'm not
any good with regex and the resulting array will have string elements
instead of numbers.
<URL:http://www.merlyn.demon.co.uk/js-valid.htm>.
Unary + .

Beware strings containing a dot not adjacent to a decimal digit.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/>? JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Aug 28 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: Gavin | last post by:
Hi, I'm a newbie to programming of any kind. I have posted this to other groups in a hope to get a response from anyone. Can any one tell me how to make my VB program read the Bios serial number...
7
by: netclectic | last post by:
Hi folks, i've searched and searched and can't find any example of what i'm trying to do. Essentially (i think) i need to add a new operator to Number. I'm using eval to evaluate expressions...
25
by: Jason | last post by:
Hi, below is example code which demonstrates a problem I have encountered. When passing a number to a function I compare it with a string's size and then take certain actions, unfortunately during...
4
by: B. Fletcher | last post by:
Hi, I'm having some trouble with javascript code of mine: When the script runs, I vget an error in line 119: Number Expected. I'm not sure as to why this is happening. Any advice would be...
23
by: Davey | last post by:
How do I display an integer in binary format in C? e.g. 4 displayed as "100"
1
by: Andrew Arace | last post by:
I searched for something like this existing already, failing to find it, I wrote it myself. If this already existed somewhere in the framework I appologize for my ignorance. This method will...
6
by: Jovo Mirkovic | last post by:
Hi, I have to make a program which will generate 100,000 different ID numbers (for example 2345-9341-0903-3432-3432 ...) It must be really different, I meen it can not be a similar (like...
1
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - Why does 1+1 equal 11? or How do I convert a string to a number?...
19
by: VK | last post by:
http://groups.google.com/group/comp.lang.javascript/browse_frm/thread/ b495b4898808fde0> is more than one month old - this may pose problem for posting over some news servers. This is why I'm...
2
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - Why does 1+1 equal 11? or How do I convert a string to a number?...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.