0

I have a string as a1234b5.

I am trying to get 1234 (in between a and b5). i tried the following way

number.replace(/[^0-9\.]/g, '');

But it's giving me like 12345. But I need 1234. how to achieve this in Javascript ?

  • You are trying to get the number by using replace? :S – Andrew McGivery Jun 04 '14 at 20:22
  • 2
    Is it always `a` and `b` that surround the numbers you want? What other rules apply to your text that we don't know of? – S.B. Jun 04 '14 at 20:22
  • @AndrewMcGivery: I think he is trying to eliminate all other characters that he does not desire. – Amberlamps Jun 04 '14 at 20:24
  • 1
    http://stackoverflow.com/questions/10003683/javascript-get-number-from-string – sjkm Jun 04 '14 at 20:25
  • @sjkm not the same thing – Huangism Jun 04 '14 at 20:26
  • @Huangism of course, its all about extracting numbers (integers) out of a string – sjkm Jun 04 '14 at 20:27
  • 1
    @sjkm but the OP does not want all of the numbers – Huangism Jun 04 '14 at 20:29
  • @Huangism It's actually a senseless discussion: Stackoverflow is here to help finding a solution and giving hints and supporting the OP and not for the OP to sit back and do nothing. The given link covers all about extracting numbers from a string or gives at least enough hints to come to a solution yourself (regex, parsing, etc.) – sjkm Jun 04 '14 at 20:39
  • @sjkm yea but it seems all the answers I see are the exact solution the OP needs. I guess people want the check marks. I am sure this applies to some of your answers as well – Huangism Jun 04 '14 at 20:42
  • @Huangism yeah, unfortunately you're perfectly right about that... – sjkm Jun 04 '14 at 20:44

5 Answers5

2

You can use:

var m = 'a1234b5'.match(/\d+/);
if (m)
   console.log(m[0]);
//=> "1234"
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • This will throw an error when there is actually no match. – Amberlamps Jun 04 '14 at 20:28
  • fixed it now. I appreciate all the comments but please understand emphasis on using right regex here. – anubhava Jun 04 '14 at 20:34
  • This still ignores the fact that he wants to get numbers between letters, all it does is return the *first number* it finds. Which means, `1a2345b` will return `1` instead of `2345`. To me this isn't the "right" regex but rather the "most simple" regex that fits for an arbitrary interpretation. – S.B. Jun 04 '14 at 21:01
0

Assuming that there are always letters around the numbers you want and that you only care about the very first group of numbers that are surrounded by letters, you can use this:

("abc123456def1234ghi123".match(/[^\d](\d+)[^\d]/) || []).pop()
// "123456"
S.B.
  • 2,920
  • 1
  • 17
  • 25
0
var number = 'a1234b5';
var firstMatch = number.match(/[0-9]+/);
var matches = number.match(/[0-9]+/g);
var without = matches.join('');
var withoutNum = Number(without);

console.log(firstMatch); // ["1234"]
console.log(matches); // ["1234","5"]
console.log(without); // "12345"
console.log(withoutNum); // 12345

I have a feeling that number is actually a hexadecimal. I urge you to update the question with more information (i.e. context) than you're providing.

zamnuts
  • 9,492
  • 3
  • 39
  • 46
0

slighty different approach

var a = "a1234b5243,523kmw3254n293f9823i32lia3un2al542n5j5j6j7k7j565h5h2ghb3bg43";
var b;

if ( typeof a != "undefined" )
{
    b = a.match( /[0-9]{2,}/g );
    console.log( b );
}

no output if a isn't set.

if a is empty => null

if somethings found => ["1234", "5243", "523", "3254", "293", "9823", "32", "542", "565", "43"]

deW1
  • 5,562
  • 10
  • 38
  • 54
  • i don't know why someone downvotes a accepted question... these people should be band for jealousy! – Dwza Jun 05 '14 at 07:58
0

It's not clear if a and b are always part of the strings you are working with; but if you want to 'extract' the number out, you could use:

var s = "a1234b5",
    res = s.match(/[^\d](\d+)[^\d]/);
// res => ["a1234b", "1234"]

then, you could reassign or do whatever. It's not clear what your intention is based on your use of replace. But if you are using replace to convert that string to just the number inside the [a-z] characters, this would work:

s.replace(/[^\d](\d+)[^\d](.*)$/, "$1")

But, that's assuming the first non-digit character of the match has nothing before it.

David Atchley
  • 1,204
  • 8
  • 10