Last active
August 29, 2015 14:16
-
-
Save hrdaya/39af655c0ce2a50dadce to your computer and use it in GitHub Desktop.
JavaScriptで正規表現(数字操作編) ref: http://qiita.com/hrdaya/items/9afafc571827358de3cd
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 文字列を整数にして返す(parseInt) | |
* | |
* 全角の数字は半角にする | |
* 全角ハイフン、全角ダッシュ、全角マイナス記号、長音符は半角ハイフンに変換 | |
* 先頭のマイナス以外のハイフンは削除 | |
* | |
* 「abcd -ー123かー456」→「-123456」 | |
* 「abc123456def」→「123456」 | |
* 「A0000123456」→「123456」 | |
* 「A0000123.456」→「123」 | |
* 「A0-000123.456」→「123」 | |
* 「A123-00123.456」→「12300123」 | |
* 「A-00000123456」→「-123456」 | |
* | |
* @param {String} str 変換したい文字列 | |
*/ | |
var toInt = function (str) { | |
str = str | |
.replace(/[0-9.]/g, function (s) { | |
return String.fromCharCode(s.charCodeAt(0) - 65248); | |
}) | |
.replace(/[‐-―ー]/g, '-') | |
.replace(/[^\-\d\.]/g, '') | |
.replace(/(?!^\-)[^\d\.]/g, ''); | |
return parseInt(str, 10); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 文字列を浮動小数点にして返す(parseFloat) | |
* | |
* 全角の数字は半角にする | |
* 全角ハイフン、全角ダッシュ、全角マイナス記号、長音符は半角ハイフンに変換 | |
* 先頭のマイナス以外のハイフンは削除 | |
* 数字以外の文字は削除 | |
* | |
* 「abcd -ー123かー4.56」→「-1234.56」 | |
* 「abc123456def」→「123456」 | |
* 「12.34.56」→「12.34」 | |
* 「A0000123456」→「123456」 | |
* 「A0000123.456」→「123.456」 | |
* 「A0-000123.456」→「123.456」 | |
* 「A123-00123.456」→「12300123.456」 | |
* 「A-00000123456」→「-123456」 | |
* | |
* @param {String} str 変換したい文字列 | |
*/ | |
var toFloat = function (str) { | |
str = str | |
.replace(/[0-9.]/g, function (s) { | |
return String.fromCharCode(s.charCodeAt(0) - 65248); | |
}) | |
.replace(/[‐-―ー]/g, '-') | |
.replace(/[^\-\d\.]/g, '') | |
.replace(/(?!^\-)[^\d\.]/g, ''); | |
return parseFloat(str); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 数字(文字列)に変換して返す | |
* | |
* 全角の数字は半角にする | |
* 全角ハイフン、全角ダッシュ、全角マイナス記号、長音符は半角ハイフンに変換 | |
* 先頭のマイナス以外のハイフンは削除 | |
* 数字以外の文字は削除 | |
* parseFloatを通したものを小数点以下numで切り捨て | |
* NaNの時は0 | |
* | |
* Number.prototype.toFixed()は小数点以下を近似値で表現する為 | |
* | |
* 引数numが指定なし、もしくは0の時 | |
* 「abcd -ー123かー4.56」→「-1234.56」 | |
* 「abc123456def」→「123456」 | |
* 「12.34.56」→「12.34」 | |
* 「A0000123456」→「123456」 | |
* 「A0000123.456」→「123.456」 | |
* 「A0-000123.456」→「123.456」 | |
* 「A123-00123.456」→「12300123.456」 | |
* 「A-00000123456」→「-123456」 | |
* | |
* 引数numが1の時 | |
* 「abcd -ー123かー4.56」→「-1234.5」 | |
* 「abc123456def」→「123456.0」 | |
* 「12.34.56」→「12.3」 | |
* 「A0000123456」→「123456.0」 | |
* 「A0000123.456」→「123.4」 | |
* 「A0-000123.456」→「123.4」 | |
* 「A123-00123.456」→「12300123.4」 | |
* 「A-00000123456」→「-123456.0」 | |
* | |
* 引数numが3の時 | |
* 「abcd -ー123かー4.56」→「-1234.560」 | |
* 「abc123456def」→「123456.000」 | |
* 「12.34.56」→「12.340」 | |
* 「A0000123456」→「123456.000」 | |
* 「A0000123.456」→「123.456」 | |
* 「A0-000123.456」→「123.456」 | |
* 「A123-00123.456」→「12300123.456」 | |
* 「A-00000123456」→「-123456.000」 | |
* | |
* @param {String} str 変換したい文字列 | |
* @param {Number} num 小数点以下の桁数(デフォルトは0「整数」、マイナスは無視) | |
* @param {Boolean} useMinus マイナスを削除するかどうか falseの時のみマイナスを削除 | |
*/ | |
var numString = function (str, num, useMinus) { | |
num = parseInt(num, 10); | |
num = isNaN(num) ? 0 : num; | |
// 一旦floatに変換 | |
str = str | |
.replace(/[0-9.]/g, function (s) { | |
return String.fromCharCode(s.charCodeAt(0) - 65248); | |
}) | |
.replace(/[‐-―ー]/g, '-') | |
.replace(/[^\-\d\.]/g, '') | |
.replace(/(?!^\-)[^\d\.]/g, ''); | |
str = parseFloat(str); | |
if (str < 0 && useMinus === false) { | |
str *= -1; | |
} | |
var split = str.toString().split('.'); | |
var dec = ''; | |
if (num > 0) { | |
if (split.length > 1) { | |
dec = '.' + (split[1] + new Array(num + 1).join('0')).slice(0, num); | |
} else { | |
dec = '.' + new Array(num + 1).join('0').slice(0, num); | |
} | |
} | |
return split[0] + dec; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 数字の桁区切り | |
* | |
* 「12345678」→「12,345,678」 | |
* 「12345678.123456」→「12,345,678.123456」 | |
* 「-1234678」→「-12,345,678」 | |
* 「12,345,678」→「12,345,678」 | |
* 「12,345,6789」→「12,345,6,789」(桁数を再計算しているわけではないので注意) | |
* 「12345678円」→「12,345,678円」 | |
* 「-12345円」→「-12,345円」 | |
* | |
* @param {String} str 変換したい文字列 | |
*/ | |
var numFormat = function (str) { | |
var split = str.split('.'); | |
split[0] = split[0].replace(/(\d)(?=(\d\d\d)+(?!\d))/g, '$1,'); | |
return split.length === 1 ? split[0] : split[0] + '.' + split[1]; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 桁区切りの数字から区切り文字の削除 | |
* | |
* 「12,345,678」→「12345678」 | |
* | |
*/ | |
var removeFigure = function (str) { | |
return str.replace(/,/g, ''); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 文字列を右からnum文字切り出す(num文字に足りない場合はpadで埋める) | |
* | |
* 引数numの指定なし、もしくは2「123」→「23」 | |
* 引数numが5「123」→「00123」 | |
* 引数numが-10 「123」→「23」 | |
* 引数numが0(変化なし)「123」→「123」 | |
* 引数numが5、padがa「123」→「aa123」 | |
* 引数numが10、padがab「123」→「bababab123」 | |
* | |
* @param {String} str 変換したい文字列 | |
* @param {Number} num 切り出す文字数(指定なしもしくは0より小さいときは2) | |
* @param {String} pad 桁数に足りない場合に左側を埋める文字(デフォルトは0) | |
*/ | |
var padSlice = function (str, num, pad) { | |
num = parseInt(num, 10); | |
num = isNaN(num) || num < 0 ? 2 : num; | |
pad = pad === undefined ? '0' : pad.toString(); | |
return (new Array(num + 1).join(pad) + str).slice(num * -1); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 文字列の左側を桁数numになるまで埋める(numを超える場合は何もしない) | |
* | |
* 引数numの指定なし、もしくは2「123」→「123」 | |
* 引数numが5「123」→「00123」 | |
* 引数numが-10 「123」→「123」 | |
* 引数numが0「123」→「123」 | |
* 引数numが5、padがa「123」→「aa123」 | |
* 引数numが10、padがab「123」→「abababab123」 | |
* | |
* @param {String} str 変換したい文字列 | |
* @param {Number} num 文字数(指定なしもしくは0より小さいときは2) | |
* @param {String} pad 桁数に足りない場合に左側を埋める文字(デフォルトは0) | |
*/ | |
var padFill = function (str, num, pad) { | |
num = parseInt(num, 10); | |
num = isNaN(num) || num < 0 ? 2 : num; | |
pad = pad === undefined ? '0' : pad.toString(); | |
while (str.length < num) { | |
str = pad + str; | |
} | |
return str; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment