By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
426,060 Members | 1,893 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 426,060 IT Pros & Developers. It's quick & easy.

get number from string

P: n/a
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
Share this Question
Share on Google+
6 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.