469,271 Members | 1,690 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,271 developers. It's quick & easy.

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 21852
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by netclectic | last post: by
4 posts views Thread by B. Fletcher | last post: by
23 posts views Thread by Davey | last post: by
1 post views Thread by Andrew Arace | last post: by
6 posts views Thread by Jovo Mirkovic | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.