RegExps : grab number inside of string

D

David

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
 
E

Evertjan.

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>
 
D

David

Evertjan. said:
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>



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
 
D

David

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>


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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top