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 ?

10
  • You are trying to get the number by using replace? :S Commented Jun 4, 2014 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? Commented Jun 4, 2014 at 20:22
  • @AndrewMcGivery: I think he is trying to eliminate all other characters that he does not desire. Commented Jun 4, 2014 at 20:24
  • 1
    stackoverflow.com/questions/10003683/… Commented Jun 4, 2014 at 20:25
  • 1
    @sjkm but the OP does not want all of the numbers Commented Jun 4, 2014 at 20:29

5 Answers 5

2

You can use:

var m = 'a1234b5'.match(/\d+/);
if (m)
   console.log(m[0]);
//=> "1234"
Sign up to request clarification or add additional context in comments.

3 Comments

This will throw an error when there is actually no match.
fixed it now. I appreciate all the comments but please understand emphasis on using right regex here.
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.
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"]

1 Comment

i don't know why someone downvotes a accepted question... these people should be band for jealousy!
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"

Comments

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.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.