0

I need your help as I'm stuck on a regular expression.

The regular expression needs to match any characters but the first number. This first number can be an integer, negative, decimal.

so I have the RegExp for that:

var b = /[-]?[0-9]+([\.][0-9]+)?/;

but when I do that in JavaScript:

var a = 'ab123ab45',
b = /[-]?[0-9]+([\.][0-9]+)?/;

a.replace(b, '');

it obviously return: abab45

But what I need, as you may understood, is the other way around. Here are some examples.

123 -> 123
123a -> 123
a123a -> 123
123ab45 -> 123
ab123ab45 -> 123
a1b2c3 -> 1
a1.2b -> 1.2
a1,2b -> 1

And I need to get that using only 1 regular expression with the replace function.

Rahil Wazir
  • 10,007
  • 11
  • 42
  • 64
FlorianB
  • 179
  • 1
  • 1
  • 11

3 Answers3

0

Try:

m = a.match(b);
console.log(m[0]);
Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
0

If you need it with replace (not with match):

var a = 'ab123ab45',
b = /.*?([-]?[0-9]+([\.][0-9]+)?).*/;

a.replace(b, '$1');
Zlatin Zlatev
  • 3,034
  • 1
  • 24
  • 32
  • Thanks a lot for your answer, was really helpful. Just a question in between: Better using the ".*?" at the beginning or this one "/[^0-9-]*?" in term of performance ? – FlorianB May 13 '14 at 09:00
  • I do have a second question, why using the "?" in ".*?" ? Thanks a lot – FlorianB May 13 '14 at 09:01
  • The question mark in .*? is a modifier to the quantifier to make it "lazy". In short it matches all symbols till the first match of the remaining of the regexp. Check http://stackoverflow.com/questions/2301285/what-do-lazy-and-greedy-mean-in-the-context-of-regular-expressions and http://www.regular-expressions.info/repeat.html Also note that .*? is not the same as [^0-9-]* as the later would fail to do a proper matching if the input sting looks like a-b12.34 Even if you put lazy modifier after [^0-9-]* it will not work properly in all cases, due to the complex nature of the remaining regexp – Zlatin Zlatev May 13 '14 at 10:19
  • Ok, I see. And yeah, didn't think about the "-" in the middle of the string. Thanks a lot ! – FlorianB May 13 '14 at 10:52
0

Try this;

var a = "a1b2c3";
a = a.replace(/^.*?([.,\d]+).*?$/, "$1");
alert(a);

LIVE DEMO

Regex Explanation

^.*?([.,\d]+).*?$

Assert position at the beginning of the string «^»
Match any single character that is not a line break character «.*?»
   Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?»
Match the regular expression below and capture its match into backreference number 1 «([.,\d]+)»
   Match a single character present in the list below «[.,\d]+»
      Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
      One of the characters “.,” «.,»
      A single digit 0..9 «\d»
Match any single character that is not a line break character «.*?»
   Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?»
Assert position at the end of the string (or before the line break at the end of the string, if any) «$»
Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268