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 = "abc13def345gh5 6.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 6 22078
Andrew Poulos wrote on 28 aug 2006 in comp.lang.javas cript:
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 = "abc13def345gh5 6.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 = 'abc13def345gh5 6.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)
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 = "abc13def345gh5 6.7fghdfgf"
/*
get number(1) returns 13
get number(2) returns 345
get number(3) returns 56.7
get number(4) returns undefined
*/]
'abc13def345gh5 6.7fghdfgf'.mat ch(/\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)(aa|ab(bb)*b a)*(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.
Evertjan. wrote:
Andrew Poulos wrote on 28 aug 2006 in comp.lang.javas cript:
>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 = "abc13def345gh5 6.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 = 'abc13def345gh5 6.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
Andrew Poulos said the following on 8/28/2006 6:51 AM:
Evertjan. wrote:
>Andrew Poulos wrote on 28 aug 2006 in comp.lang.javas cript:
>>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 = "abc13def345gh5 6.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 = 'abc13def345gh5 6.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.javas cript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Randy Webb wrote on 28 aug 2006 in comp.lang.javas cript:
>><script type='text/javascript'>
var s = 'abc13def345gh5 6.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)
JRS: In article <44f2b861$0$107 87$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*****@hotmai l.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.demo n.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.c om/faq/>? JL/RC: FAQ of news:comp.lang. javascript
<URL:http://www.merlyn.demo n.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demo n.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
(or would HDD be better, or both?) and put that info into VB prog so the
program won't work on another computer. My program uses an MSAccess table.
Much appreciated if you can help!
Thanks
|
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 that can be entered by users,
these expressions are defined by another app so i can't change the
format of the expressions. I've managed to support the majority of
operators by supplying my own functions that wrap the equivalent Math
functions but...
|
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 the testing of it I
discovered that negative numbers were being treated as if they were > 0. I
compiled the following on mingw compiler/dev c++/windows xp.
If I replace string.size() for a ordinary number it behaves as expected? I
notice...
|
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 helpful.
The source code is at the bottom of the post.
Thankyou very much,
|
by: Davey |
last post by:
How do I display an integer in binary format in C?
e.g. 4 displayed as "100"
| |
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 take any number within the bounds of an int
(int.MinValue - int.MaxValue) inclusive and converts it to an english
number.
For example, it will take "1" and give you "one", "2" yields "two",
"-475918" give you a whopping "negative four hundred...
|
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
2345-9341-0903-3432-343<b>1</b>) Does exist some algorithm for that...
Thanks.
|
by: FAQ server |
last post by:
-----------------------------------------------------------------------
FAQ Topic - Why does 1+1 equal 11? or How do I convert a string to a number?
-----------------------------------------------------------------------
Javascript variables are loosely typed: the conversion between a
string and a number happens automatically. Since plus (+) is also
used as in string concatenation, « '1' + 1 » is equal to « '11' »: the
String deciding...
|
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 starting a new one]
I'd still like to finish this rounding mess. As a startup lemma we can
take that VK is the worst programmer of all times and places: let's
move from here forward please.
The usability of any program depends on exact behavior...
|
by: FAQ server |
last post by:
-----------------------------------------------------------------------
FAQ Topic - Why does 1+1 equal 11? or How do I convert a
string to a number?
-----------------------------------------------------------------------
Javascript variables are loosely typed: the conversion between a
string and a number happens automatically. Since plus (+) is also
used as in string concatenation, ` '1' + 1 ` is equal to ` '11' `: the
String deciding...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |