1

For example:

"10.0.cm" goes to "10.0"

and

"10.0.m" goes to "10.0"

and

"3" stays as "3"

etc...

I tried this:

values[3].replace(/[^0-9.,]+/, '')

but this still leaves the "." after the number, eg: 10.0.

Thanks for your help...

1
  • This worked... value.match(/\d+(\.\d+)?/)[0] Commented Jul 12, 2015 at 0:57

3 Answers 3

2

The following regex should solve it

values[3].replace(/\.+[a-z]*$/, '')
Sign up to request clarification or add additional context in comments.

1 Comment

thanks, that leaves me with the bit that I don't want though : "10.0.cm".replace(/[0-9]+\.*[0-9]*/, '') - ".cm" how do swap that around?
2

var values = ["10.0.cm", "10.0.m", "3"];
var patten = /\d+(\.\d+)?/g;
for (var i = 0; i < values.length; i++) {
    console.info(values[i].match(patten));
}

1 Comment

This will return a length 2 Array which contains two .s if given a String like 'foo_1.0.bar.1.2.xyz'
1

The problem you've described isn't a simple "find numbers in a String"

It's not easy to do this in one step because RegExp in JavaScript doesn't support lookbacks. However

  • It's pretty easy to remove all characters which aren't a digit or .
  • It's pretty easy to find specific chars in a String
  • It's pretty easy to build a String
var str = 'foo_1.0.bar.1.2.xyz';

str = str.replace(/[^\d\.]/g, '').split('.');
str = str[0] ? str[0] + '.' + str.slice(1).join('') : '';

str; // "1.012"

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.