Hatena::ブログ(Diary)

babu_babu_babooのごみ箱

2018-04-23

気づいたら朝じゃないか!やべ〜寝よっと。 ほぼ完成か?

17:42

マイナスの符号を忘れてた。


仕事までに時間があるので符号付きにも対応した。あとはバグ取り。

これで input 要素の value 値に、3桁カンマとか単位が入り込んでいても気兼ねなく PHP に渡すことができる。

ほぼ同じ正規表現が使い回せるので OK!


ついでに教えてgoo に質問した。

https://oshiete.goo.ne.jp/qa/10451407.html

きっと逃げのお言葉を聞けると思って^^;

しばらく要素を見ようっと。では仕事!仕事!

<!DOCTYPE html>
<html lang="ja">
<meta charset="UTF-8">
<title>数値文字列から数値部分を配列にして抜き取る</title>
<style>
table { width:100%; }
caption {
  font: normal small monospace;
  text-align: left;
}
tr.red {
  background: #f88;
}
td {
  text-align:right;
}
</style>
<body>
<table border=1></table>
<script>

let ary = [
                   '',       // => null
                  '0',       // => [,,,,0,]
                  '1',       // => [,,,,1,]
                 '12',       // => [,,,,12,]
                '123',       // => [,,,,123,]
              '1,234',       // => [,,,1,234,]
             '12,345',       // => [,,,12,345,]
            '123,456',       // => [,,,123,456,]
              '12,34',       // => null
             '123,45',       // => null
        '123,456,789',       // => [,,123,456,789,]
    '123,123,456,789',       // => [,123,123,456,789,]
    '123123123456789',       // => [123,123,123,456,789,]
                '123円',     // => [,,,,123,]
              '1,234 円',    // => [,,,1,234,]
          '1,234,567 円',    // => [,,1,234,567,]
                '123.00',    // => [,,,,123,00]
              '12345.00123', // => [,,,12,345,00123]
             '$ 1234',       // => [,,,1,234,]
        '$ 1,234,567.',      // => [,,1,234,567,]
                  '0.0001',  // => [,,,,0,0001]
                   '.123',   // => [,,,,,123]

                   '',       // => null
                  '-0',       // => [,,,,0,]
                  '-1',       // => [,,,,1,]
                 '-12',       // => [,,,,12,]
                '-123',       // => [,,,,123,]
              '-1,234',       // => [,,,1,234,]
             '+12,345',       // => [,,,12,345,]
            '-123,456',       // => [,,,123,456,]
              '-12,34',       // => null
             '-123,45',       // => null
        '-123,456,789',       // => [,,123,456,789,]
    '-123,123,456,789',       // => [,123,123,456,789,]
    '-123123123456789',       // => [123,123,123,456,789,]
                '-123円',     // => [,,,,123,]
              '-1,234 円',    // => [,,,1,234,]
          '-1,234,567 円',    // => [,,1,234,567,]
                '-123.00',    // => [,,,,123,00]
              '-12345.00123', // => [,,,12,345,00123]
             '$ -1234',       // => [,,,1,234,]
        '$ -1,234,567.',      // => [,,1,234,567,]
                  '0.0001',  // => [,,,,0,0001]
                   '.123',   // => [,,,,,123]
];

let
  notNull     = '(?!^$)',
  head_digit  = '[1-9][0-9]{0,2}',
  after_digit = '[0-9]{3}',
  word        = '\\s*[^0-9\\.\\,\\-]*\\s*',
  word_last   = '\\s*[^0-9\\.\\,]*\\s*',
  comma       = '\\,',
  point       = '\\.',
  decimal     = '[0-9]{0,}',
  OR          = '|';

let
  assumption  = comma + '?' + after_digit,
  number_part = '(' + head_digit + OR + after_digit + ')' + comma + '?',
  decimal_    = '(?:' + point + '(' + decimal + '))?',
  sign        = '(?:\\+|(\\-))?';

let
  base        = '(?:(' + '^0' + OR + '' + head_digit + OR + after_digit + ')?' + decimal_ + ')',
  kiro        = '(?:' + number_part + '(?=' + after_digit + '))?',
  mega        = '(?:' + number_part + '(?=' + after_digit + assumption + '))?',
  giga        = '(?:' + number_part + '(?=' + after_digit + assumption + assumption + '))?',
  tera        = '(?:' + number_part + '(?=' + after_digit + assumption + assumption + assumption + '))?';

let
  reg = new RegExp ( '^' + notNull + word + sign + tera + giga + mega + kiro + base + word_last + '$');


//__________________________________

let table = document.querySelector ('table');

table.createCaption ().textContent = reg;
for (let a of ary) {
  let
    tr = table.insertRow (-1),
    r = reg.exec(a);
  
  r
  ? (r.splice (0, 0, 'o'), tr.className = 'red')
  : r = ['x', a,,,,,,,,];
  
  for (let c of r)
    tr.insertCell(-1).textContent = c;
}

</script>