469,889 Members | 1,106 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

RegExps : grab number inside of string

Hi Everyone,

I'm looking for a way to grab a number from a string, although this number
is not at either end of the string.

Here is the string in question: var el = "v4Function_Name('argument3')"

Here is a substring that returns the number ( the 3 ) successfully...
var number = el.substring(el.lastIndexOf("ent")+3,el.length-2);

Seems very ineffecient to do it this way, and better to find a lean elegant
way using replace(); How does one go about getting a value inside of a
string with regular expressions? .. or is there a better way?

Here are a couple of the many expressions I have failed with...

var pat = /^[^ent][0-9]*$"/;
var pat = /v4Function_Name\('argument([^0-9]+)'/;
var number = el.replace(pat,"");

David
Oct 18 '07 #1
3 2590
David wrote on 18 okt 2007 in comp.lang.javascript:
Hi Everyone,

I'm looking for a way to grab a number from a string, although this
number is not at either end of the string.

Here is the string in question: var el =
"v4Function_Name('argument3')"

Here is a substring that returns the number ( the 3 ) successfully...
var number = el.substring(el.lastIndexOf("ent")+3,el.length-2);

Seems very ineffecient to do it this way, and better to find a lean
elegant way using replace(); How does one go about getting a value
inside of a string with regular expressions? .. or is there a better
way?

Here are a couple of the many expressions I have failed with...

var pat = /^[^ent][0-9]*$"/;
var pat = /v4Function_Name\('argument([^0-9]+)'/;
I do not understand why you would re-var a variable
var number = el.replace(pat,"");
Try:

<script type='text/javascript'>

var s = "v421Function_Name('argument333')";
// s = 'test for no numbers';

var n = s.match(/\d+/g);
if (!n) n = ['none'];
alert( 'all the numbers: ' + n.join(',') );

var n = s.match(/\d+/g);
if (!n) n = ['none'];
alert( 'first number: ' + n[0] );

var n = s.match(/\d+/g);
if (!n) n = ['none'];
alert( 'last number: ' + n.pop() );

</script>

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Oct 18 '07 #2

"Evertjan." <ex**************@interxnl.netwrote in message
news:Xn********************@194.109.133.242...
David wrote on 18 okt 2007 in comp.lang.javascript:
>Hi Everyone,

I'm looking for a way to grab a number from a string, although this
number is not at either end of the string.

Here is the string in question: var el =
"v4Function_Name('argument3')"

Here is a substring that returns the number ( the 3 ) successfully...
var number = el.substring(el.lastIndexOf("ent")+3,el.length-2);

Seems very ineffecient to do it this way, and better to find a lean
elegant way using replace(); How does one go about getting a value
inside of a string with regular expressions? .. or is there a better
way?

Here are a couple of the many expressions I have failed with...

var pat = /^[^ent][0-9]*$"/;
var pat = /v4Function_Name\('argument([^0-9]+)'/;

I do not understand why you would re-var a variable
>var number = el.replace(pat,"");

Try:

<script type='text/javascript'>

var s = "v421Function_Name('argument333')";
// s = 'test for no numbers';

var n = s.match(/\d+/g);
if (!n) n = ['none'];
alert( 'all the numbers: ' + n.join(',') );

var n = s.match(/\d+/g);
if (!n) n = ['none'];
alert( 'first number: ' + n[0] );

var n = s.match(/\d+/g);
if (!n) n = ['none'];
alert( 'last number: ' + n.pop() );

</script>

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)


HI Evertjan,

I didn't re var the var, those were 2 examples I tried that didn't work.
"Here are a couple of the many expressions I have failed with".

Your example works great. Thanks for the understanding.

David



Oct 18 '07 #3
>Try:

<script type='text/javascript'>

var s = "v421Function_Name('argument333')";
// s = 'test for no numbers';

var n = s.match(/\d+/g);
if (!n) n = ['none'];
alert( 'all the numbers: ' + n.join(',') );

var n = s.match(/\d+/g);
if (!n) n = ['none'];
alert( 'first number: ' + n[0] );

var n = s.match(/\d+/g);
if (!n) n = ['none'];
alert( 'last number: ' + n.pop() );

</script>

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

By the way, the numbers will always be there, just different, so I modified
your example a bit.

string.match(/\d+/g).pop();

Nice simple and effecient method Evertjan, thanks you for your help.

David


Oct 18 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by R. Tarazi | last post: by
6 posts views Thread by David | last post: by
5 posts views Thread by Digital.Rebel.18 | last post: by
4 posts views Thread by strawberry | last post: by
4 posts views Thread by David | last post: by
4 posts views Thread by sake | last post: by
2 posts views Thread by dschu012 | last post: by
1 post views Thread by Waqarahmed | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.