Last active
August 29, 2015 14:16
-
-
Save hrdaya/1bf6255e5e50aeeff0fe to your computer and use it in GitHub Desktop.
JavaScriptで正規表現(文字列制限編) ref: http://qiita.com/hrdaya/items/2cd5cc19cae35061225c
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数のスペースに変換後、改行以外の制御文字を削除 | |
* 改行文字は\nに変換 | |
* | |
* @param {String} str 変換したい文字列 | |
* @param {Number} num スペースの文字数(デフォルトは4) | |
*/ | |
var removeCtl = function (str, num) { | |
num = parseInt(num, 10); | |
var space = new Array(isNaN(num) ? 5 : num + 1).join(' '); | |
return str | |
.replace(/\t/g, space) | |
.replace(/[\x00-\x09\x0b-\x1f\x7f-\x9f]/g, '') | |
.replace(/\x0a/g, '\n'); | |
}; |
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
/** | |
* 改行の削除 | |
* | |
* @param {String} str 変換したい文字列 | |
*/ | |
var removeNl = function (str) { | |
return str.replace(/\n/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
/** | |
* 数字とコロン(全角半角変換あり) | |
* | |
* @param {String} str 変換したい文字列 | |
**/ | |
var numColon = function (str) { | |
return str.replace(/[0-9:]/g, function (s) { | |
return String.fromCharCode(s.charCodeAt(0) - 65248); | |
}).replace(/[^\d:]/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
/** | |
* ひらがなとスペース・長音符・半角ハイフン・半角数字のみ | |
* 数字を半角に変換 | |
* 全角スペースを半角スペースに変換 | |
* 全角ハイフン、全角ダッシュ、全角マイナス記号は半角ハイフンに変換 | |
* | |
* @param {String} str 変換したい文字列 | |
**/ | |
var hiragana = function (str) { | |
return str | |
.replace(/[0-9]/g, function (s) { | |
return String.fromCharCode(s.charCodeAt(0) - 65248); | |
}) | |
.replace(/゙/g, '゛') | |
.replace(/゚/g, '゜') | |
.replace(/ /g, ' ') | |
.replace(/[‐-―]/g, '-') | |
.replace(/[^ぁ-ゔーゝゞゕゖ゛゜ \d\-]/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
/** | |
* カタカナとスペース・長音符・半角ハイフン・半角数字のみ | |
* 数字を半角に変換 | |
* 全角スペースを半角スペースに変換 | |
* 全角ハイフン、全角ダッシュ、全角マイナス記号は半角ハイフンに変換 | |
* | |
* @param {String} str 変換したい文字列 | |
**/ | |
var katakana = function (str) { | |
return str | |
.replace(/[0-9]/g, function (s) { | |
return String.fromCharCode(s.charCodeAt(0) - 65248); | |
}) | |
.replace(/゙/g, '゛') | |
.replace(/゚/g, '゜') | |
.replace(/ /g, ' ') | |
.replace(/[‐-―]/g, '-') | |
.replace(/[^ァ-ヴーヽヾヵヶヷヸヹヺ゛゜ \d\-]/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
/** | |
* 全角スペースを含めたトリム | |
* 「m」フラグを使用すると連続した改行が削除されることへの対策版 | |
* | |
* @param {String} str 変換する文字列 | |
* @param {Boolean} multipleLines 各行ごとにトリムするかどうか(trueの時のみ各行トリム) | |
* @param {Boolean} useBlankLine 空行を使用するかどうか(falseの時は空行削除) | |
*/ | |
var trim = function (str, multipleLines, useBlankLine) { | |
var reg = /^[\s ]+|[ \s]+$/g; | |
if (multipleLines === true) { | |
if (useBlankLine === false) { | |
str = str.replace(/^[\s ]+|[ \s]+$/gm, ''); | |
} else { | |
str = str | |
.split('\n') | |
.map(function (line) { | |
return line.replace(reg, ''); | |
}) | |
.join('\n') | |
.replace(reg, ''); | |
} | |
} else { | |
str = str.replace(reg, ''); | |
} | |
return 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
/** | |
* 全角スペースを含めた右トリム | |
* 「m」フラグを使用すると連続した改行が削除されることへの対策版 | |
* | |
* @param {String} str 変換する文字列 | |
* @param {Boolean} multipleLines 各行ごとにトリムするかどうか(trueの時のみ各行トリム) | |
* @param {Boolean} useBlankLine 空行を使用するかどうか(falseの時は空行削除) | |
*/ | |
var rtrim = function (str, multipleLines, useBlankLine) { | |
var reg = /[ \s]+$/g; | |
if (multipleLines === true) { | |
if (useBlankLine === false) { | |
str = str | |
.replace(/[ \s]+$/gm, '') | |
.replace(/^\n+/g, ''); | |
} else { | |
str = str | |
.split('\n') | |
.map(function (line) { | |
return line.replace(reg, ''); | |
}) | |
.join('\n') | |
.replace(reg, ''); | |
} | |
} else { | |
str = str.replace(reg, ''); | |
} | |
return 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
/** | |
* 全角スペースを含めた左トリム | |
* 「m」フラグを使用すると連続した改行が削除されることへの対策版 | |
* | |
* @param {String} str 変換する文字列 | |
* @param {Boolean} multipleLines 各行ごとにトリムするかどうか(trueの時のみ各行トリム) | |
* @param {Boolean} useBlankLine 空行を使用するかどうか(falseの時は空行削除) | |
*/ | |
var ltrim = function (str, multipleLines, useBlankLine) { | |
var reg = /^[\s ]+/g; | |
if (multipleLines === true) { | |
if (useBlankLine === false) { | |
str = str | |
.split('\n') | |
.map(function (line) { | |
return line.replace(reg, ''); | |
}) | |
.join('\n') | |
.replace(reg, '') | |
.replace(/[\n\n]+/g, '\n') | |
.replace(/\n+$/g, ''); | |
} else { | |
var split = str.split('\n'); | |
str = split.map(function (line) { | |
return line.replace(reg, ''); | |
}).join('\n').replace(reg, ''); | |
} | |
} else { | |
str = str.replace(reg, ''); | |
} | |
return 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
/** | |
* 英字のみ(全角半角変換あり) | |
* | |
* @param {String} str 変換したい文字列 | |
*/ | |
var alpha = function (str) { | |
return str.replace(/[A-Za-z]/g, function (s) { | |
return String.fromCharCode(s.charCodeAt(0) - 65248); | |
}).replace(/[^A-Za-z]/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
/** | |
* 数字のみ(全角半角変換あり) | |
* | |
* @param {String} str 変換したい文字列 | |
*/ | |
var num = function (str) { | |
return str.replace(/[0-9]/g, function (s) { | |
return String.fromCharCode(s.charCodeAt(0) - 65248); | |
}).replace(/[^\d]/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
/** | |
* 英数字のみ(全角半角変換あり) | |
* | |
* @param {String} str 変換したい文字列 | |
*/ | |
var alphaNum = function (str) { | |
return str.replace(/[A-Za-z0-9]/g, function (s) { | |
return String.fromCharCode(s.charCodeAt(0) - 65248); | |
}).replace(/[^A-Za-z\d]/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
/** | |
* 数字とハイフンのみ(全角半角変換あり) | |
* 全角ハイフン、全角ダッシュ、全角マイナス記号、長音符は半角ハイフンに変換 | |
* | |
* @param {String} str 変換したい文字列 | |
*/ | |
var numPyphen = function (str) { | |
return str.replace(/[0-9]/g, function (s) { | |
return String.fromCharCode(s.charCodeAt(0) - 65248); | |
}).replace(/[‐-―ー]/g, '-').replace(/[^\d\-]/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
/** | |
* 数字とスラッシュ(全角半角変換あり) | |
* | |
* @param {String} str 変換したい文字列 | |
**/ | |
var numSlash = function (str) { | |
return str.replace(/[0-9/]/g, function (s) { | |
return String.fromCharCode(s.charCodeAt(0) - 65248); | |
}).replace(/[^\d\/]/g, ''); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment