473,387 Members | 1,456 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 2678
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: R. Tarazi | last post by:
Hello together, I'm having extreme difficulties using RegExps for a specific problem and would really appreciate any help and hope somebody will read through my "long" posting... 1. <?php...
6
by: David | last post by:
Hi all, I have a function that produces a string within it with a root relative path... /mysite/images/ The problem is that this is being "seen" as a RegExp when processing it further in...
5
by: Digital.Rebel.18 | last post by:
I'm trying to figure out how to extract the keywords from an HTML document. The input string would typically look like: <meta name='keywords' content='word1, more stuff, etc'> Either single...
4
by: strawberry | last post by:
Say I have a chunk of text and I know that somewhere in that chunk ot text is the phrase 'Next Week's Winning Number will be:', is there a way I can automatically grab the string immediately...
4
by: possibilitybox | last post by:
I'm trying to make a unicode friendly regexp to grab sentences reasonably reliably for as many unicode languages as possible, focusing on european languages first, hence it'd be useful to be able...
4
by: David | last post by:
Is there a way to find which element in an object array the keyword 'this' is acting upon? For example: function doMe(){ var aTags = document.getElementById("list").getElementsByTagName("a");...
2
by: Yorian | last post by:
I just started to try regexps in php and I didn't have too many problems, however I found a few when trying to build a templte engine. The first one is found is the dollar sign. In my template I...
4
by: sake | last post by:
Hi, Once again, Google has failed me. I need to know how to parse a string that contains two integer numbers separated by a comma (ex: 6,3). Even though the two numbers are supposed to be single...
2
by: dschu012 | last post by:
There are images from the web that are PNG's that I am trying to grab. I seem to be able to grab them however when I do they are not the same size (in bytes) as the image that is stored on the web....
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.