サンプル
期待値
- あ : あいうえお
- 先頭、ひらがな
- お : あいうえお
- ひらがな、終端
- c : ccccccc
- 先頭、アルファベット
- あ : (改行)あいうえお
- 先頭、ひらがな
- お : …うえお(改行)かきく…
- ひらがな、終端
文字の前後の文字種を判定する。
参考:javascriptのmatchで質問です。 - OKWave
前回からの変更点。
自分でも何がしたかったのかさっぱり。
改行付きのチェックは無理でした。
期待値
<input type="button" value="判定" onclick="judge()">
<script>
"use strict";
(function(window, document, undefined){
function $(id, val){
var el = document.getElementById(id);
if( val!==undefined) {
val = val.replace(/\\n/g, "\n");
el.value=val;
}
return el.value;
}
var Judge = (function(){
var _results = [];
var _methods = [];
function Judge(searchLetter, text, option) {
if( this.constructor !== Judge ){
return new Judge(searchLetter, text, option);
}
this.source = escapeRegExpString(searchLetter);
this.leftContext = ''; // 前文字
this.rightContext = ''; // 後文字
this.$1 = ''; // 検査結果 前
this.$2 = ''; // 検査結果 後
if( arguments.length > 1 ){
this.exec(text, option);
//_judge.call(this, text, option);
return [this.$1, this.$2];
}
return this;
};
Judge.add = function(pattern, type){
_results.push(arguments[arguments.length-1]);
_methods.push(arguments.length>2 ? Array.prototype.slice.call(arguments, 0, arguments.length-1) : pattern);
};
Judge.prototype = {
constructor: Judge,
exec: function(text, option){
option = String(option) || "";
text = String(text) || "";
_judge.call(this, text, option);
return (this.$1 !== undefined || this.$2 !== undefined);
},
toString: function(){
return this.source;
}
};
/****
text: String
option: String
****/
function _judge(text, option){
var len = ( option.match(/1/) )? c.length: 1;
var c = this.source.substring(0, len);
if( option.match(/m/) ){
}
var r1, r2;
var code = c.charCodeAt(0);
var leftCode = -1, rightCode = -1;
var re = text.match( RegExp("(^|.?)"+c+"(.?|$)") );
if( re ){
leftCode = re[1].charCodeAt(0),
rightCode = re[2].charCodeAt(0);
this.leftContext = re[1];
this.rightContext = re[2];
}
re && _methods.forEach(function(item, index, array){
if( item.constructor === Array ){
if( item[0] <= leftCode && leftCode <= item[1] ){
r1 = _results[index];
}
if( item[0] <= rightCode && rightCode <= item[1] ){
r2 = _results[index];
}
}else if( item.constructor === RegExp ){
if( item.toString() === "/^/" ){
r1 = _isFirst(c, text)? _results[index]: r1;
}else if( item.toString() === "/$/" ){
r2 = _isLast(c, text)? _results[index]: r2;
}else{
if( !r1 && item.exec(re[1]) ){
r1 = _results[index];
}
if( !r2 && item.exec(re[2]) ){
r2 = _results[index];
}
}
}
});
this.$1 = r1;
this.$2 = r2;
return [r1, r2];
}
function escapeRegExpString(s){
return s.replace(/([\.+*[|?{<>:=-])/g, "\\$1");
}
function _isFirst(c, text){
return text.indexOf(c)===0;
}
function _isLast(c, text){
return text.indexOf(c)===text.length-1;
}
return Judge;
})();
function judge(){
var c = $('ipt_c'),
text = $('ipt_text'),
result = '';
var judge = new Judge(c);
var judge_result1 = judge.exec(text);
if( !judge_result1 ){
result += "マッチしません。";
}else if( judge.$1 === judge.$2 ){
result += judge.$1 + "です。";
}else{
result += "前が" + judge.$1 + "("+judge.leftContext + ")"+ "で後ろが" + judge.$2 + "("+judge.rightContext+")" + "です。";
}
result += "\n";
$('ipt_out', result);
}
window.Judge = Judge;
window.$ = $;
window.judge = judge;
})(this, document, void(0));
Judge.add(/[a-zA-Z]/, "アルファベット");
Judge.add(/[あ-ん]/, "ひらがな");
Judge.add(/[ \s]/, "空白");
Judge.add(/^/, "先頭");
Judge.add(/$/, "終端");
Judge.add(128, 256, "半角カナ");
Judge.add(65393, 65473, "半角カナ");
</script>