extracting numbers from a string - there will be better ways?!

G

geoffcox75

Hello,

I have written the following to get numbers out of an id value (var
div) - the first 3 values of div can be between 001 and 999, the
fourth between 1 and 4.

Any suggestions for better ways please?

Cheers,

Geoff

var div = "div0294";

var sn = div.substring(3,6);

if ( (sn.charAt(0) == 0) && (sn.charAt(1) == 0) ) {
var sn1 = sn.charAt(2);
alert("sn1 = " + sn1);
} else if ((sn.charAt(0) == 0) && (sn.charAt(1) != 0)) {
var sn2 = sn.substring(1,3);
alert("sn2 = " + sn2);
} else {
var sn3 = sn.substring(0,3);
alert("sn3 = " + sn3);
}

var inum = div.substring(6,7);
alert("inum = " + inum);
 
T

Thomas 'PointedEars' Lahn

geoffcox75 said:
I have written the following to get numbers out of an id value (var
div) - the first 3 values of div can be between 001 and 999, the
fourth between 1 and 4.

Any suggestions for better ways please?
[...]
var div = "div0294";
[...]

This is a general approach that finds the first decimal number in any
string; take your pick:

var n;
if (isNaN(n = parseInt(div, 10)))
{
div = div.replace(/^\D+/, "");
n = parseInt(div, 10);
}

console.log(n);


HTH

PointedEars
 
R

RobG

geoffcox75 wrote:
I have written the following to get numbers out of an id value (var
div) - the first 3 values of div can be between 001 and 999, the
fourth between 1 and 4.
Any suggestions for better ways please?
[...]
var div = "div0294";
[...]
This is a general approach that finds the first decimal number in any
string; take your pick:
 var n;
 if (isNaN(n = parseInt(div, 10)))
 {
   div = div.replace(/^\D+/, "");
   n = parseInt(div, 10);
 }
 console.log(n);

I should have said that div for example if div = "div1234" I need to
get 2 numbers out of the string, 123 and 4.

Then Thomas got you pretty close. Use the RegExp to remove leading
non-digits, then use substring to get the last digit. Use either
parseInt or Number() to convert the strings to numbers if required,
e.g.

var id = 'div01294'
var s = id.replace(/\D/g, '');
var len = s.length - 1;
var n1 = s.substring(0, len);
var n2 = s.substring(len);
alert(n1 + ' : ' + n2);

You can use various methods to convert n1 and n2 to Numbers if
required, such as parseInt(n1, 10), Number(n1), +n1 or use it in a
mathematic operation other than addition.
 
T

Thomas 'PointedEars' Lahn

Geoff said:
Thomas said:
geoffcox75 said:
I have written the following to get numbers out of an id value (var
div) - the first 3 values of div can be between 001 and 999, the
fourth between 1 and 4.

Any suggestions for better ways please?
[...]
var div = "div0294";
[...]

This is a general approach that finds the first decimal number in any
string; take your pick:

var n;
if (isNaN(n = parseInt(div, 10)))
{
div = div.replace(/^\D+/, "");
n = parseInt(div, 10);
}

console.log(n);

I should have said that div for example if div = "div1234" I need to
get 2 numbers out of the string, 123 and 4.

In that case, try this:

var div = "div1234abc";
var m = div.match(/(\d+)(\d)\D*$/);

console.log(m[1], m[2]);

You can parseInt(, 10) the matches, if necessary.

Please trim your quotes, and don't quote signatures unless required;
see <http://jibbering.com/faq/#posting>.


PointedEars
 
D

Dr J R Stockton

In that case, try this:

  var div = "div1234abc";
  var m = div.match(/(\d+)(\d)\D*$/);

  console.log(m[1], m[2]);

You can parseInt(, 10) the matches, if necessary.

It can only be necessary, given that m[1] and m[2] are decimal digit
strings, if +"0123" would ever be interpreted as Octal.

My chronologically-previous post was prematurely sent. Include
 
T

Thomas 'PointedEars' Lahn

Dr said:
Thomas said:
In that case, try this:
var div = "div1234abc";
var m = div.match(/(\d+)(\d)\D*$/);

console.log(m[1], m[2]);

You can parseInt(, 10) the matches, if necessary.

It can only be necessary, given that m[1] and m[2] are decimal digit
strings, if +"0123" would ever be interpreted as Octal.

We don't know what the OP is going to do with the matches, hence my
conditional wording.
My chronologically-previous post was prematurely sent. Include

(sic!)

You were saying?


PointedEars
 
R

RoLo

Hello,

I have written the following to get numbers out of an id value (var
div) - the first 3 values of div can be between 001 and 999, the
fourth between 1 and 4.

Any suggestions for better ways please?

Cheers,

Geoff

var div = "div0294";

var sn = div.substring(3,6);

if ( (sn.charAt(0) == 0) && (sn.charAt(1) == 0) ) {
var sn1 = sn.charAt(2);
alert("sn1 = " + sn1);} else if ((sn.charAt(0) == 0) && (sn.charAt(1) != 0)) {

var sn2 = sn.substring(1,3);
alert("sn2 = " + sn2);} else {

var sn3 = sn.substring(0,3);
alert("sn3 = " + sn3);

}

var inum = div.substring(6,7);
alert("inum = " + inum);

alert((('a1b2c3d').match(/\d/g)).join('')); // '123'
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]>,
Wed, 21 Jan 2009 14:44:26, Thomas 'PointedEars' Lahn
Dr said:
Thomas 'PointedEars' Lahn wrote:
You can parseInt(, 10) the matches, if necessary.

It can only be necessary, given that m[1] and m[2] are decimal digit
strings, if +"0123" would ever be interpreted as Octal.

We don't know what the OP is going to do with the matches, hence my
conditional wording.

We don't know. But it can only be necessary to use parseInt if it is
necessary to convert to Number *AND* unary + cannot be used - for which
the only reasonable possibility is as indicated. Your understanding of
English is not, in conjunction with the haste in which you operate, as
good as you think.
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top