最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Regex replace string which is not float - Stack Overflow

programmeradmin0浏览0评论

I have a string, where I need to parse it as a float, but first I need to replace it, if it is not a number (an integer or a float), so I am trying to create an regular expression to do it

My tries results in NaN

One of my best tries is

var $replace = $text.replace(/^[^d.]*/, '');
var $float = parseFloat($replace);

Can anybody tell me, what I am doing wrong?

I have a string, where I need to parse it as a float, but first I need to replace it, if it is not a number (an integer or a float), so I am trying to create an regular expression to do it

My tries results in NaN

One of my best tries is

var $replace = $text.replace(/^[^d.]*/, '');
var $float = parseFloat($replace);

Can anybody tell me, what I am doing wrong?

Share Improve this question edited Nov 29, 2011 at 22:24 Christofer Eliasson 33.9k7 gold badges77 silver badges103 bronze badges asked Nov 29, 2011 at 22:00 The87BoyThe87Boy 8874 gold badges15 silver badges32 bronze badges 7
  • your regex says to replace zero or more characters that are not a d or a . from the beginning of your string with nothing... What are you trying to replace/remove exactly? – J. Holmes Commented Nov 29, 2011 at 22:06
  • I am trying to replace everything, that are not a digit (or a . (used in a float)) – The87Boy Commented Nov 29, 2011 at 22:13
  • Why are you prepending a dollar sign to your variables? – NullUserException Commented Nov 29, 2011 at 22:26
  • Can you give some examples of what $text might look like, and what you want $replace and $float to look like in those cases? – ruakh Commented Nov 30, 2011 at 15:12
  • @NullUserException It depends on, which server-side programming language, I am using, and right here I am using PHP, which uses dollar signs in variables – The87Boy Commented Dec 4, 2011 at 20:05
 |  Show 2 more ments

3 Answers 3

Reset to default 2

If you really want to replace everything thats not a digit, then try this:

var $replace = $text.replace(/[^\d.]/g, '');
var $float = parseFloat($replace);

This will replace a string of "123a3d2" with a string of "12332".

It looks like you want to strip "non-numeric" characters from the beginning of the string before converting it to float. A naive approach would be:

var s = input.replace(/^[^\d.]+/, '');
var n = parseFloat(s);

This works for inputs like "foo123" but will fail on "foo.bar.123". To parse this we need a more sophisticated regexp:

var s = input.replace(/^(.(?!\d)|\D)+/, '');
var n = parseFloat(s);

Another method is to strip the input char by char until we find a valid float:

function findValidFloat(str) {
    for (var i = 0; i < str.length; i++) {
        var f = parseFloat(str.substr(i))
        if (!isNaN(f))
            return f;
    }
    return NaN;
}
if (! isNaN($text))
  $float = parseFloat($text);
else
  $float = 0; // or whatever
发布评论

评论列表(0)

  1. 暂无评论