比如以下的代碼就是用來測試用正則表達式匹配從 0xff 到 0xffff 的字符。而值范圍在 0 到 0xfe 的所有字符是不能被匹配的。
以下為引用的內容:
復制代碼 代碼如下:
Regex regex = new Regex(@"[/u00FF-/uFFFF]+");
// The characters, whoes value are smaller than 0xff,
// are not expected to be matched.
for (int i = 0; i <0xff; i++) {
string s = new string(new char[] { (char)i });
Debug.Assert(!regex.IsMatch(s), string.Format(
"The character was not expected to be matched: 0x{0:X}!", i));
}
// However, the characters whoes value
// are greater than 0xfe are expected to be matched.
for (int i = 0xff; i <= 0xffff; i++) {
string s = new string(new char[] { (char)i });
Debug.Assert(regex.IsMatch(s), string.Format(
"The character was expected to be matched: 0x{0:X}!", i));
}
復制代碼 代碼如下:
var re = /[/u00FF-/uFFFF]+/;
// var re = /[/u00FF-/uFFFF]+/i;
for(var i=0; i<0xff; i++) {
var s = String.fromCharCode( i );
if ( re.test(s) ) {
alert( 'Should not be matched: ' + i + '!' );
}
}
for(var i=0xff; i<=0xffff; i++) {
var s = String.fromCharCode( i );
if ( !re.test(s) ) {
alert( 'Should be matched: ' + i + '!' );
}
}
新聞熱點
疑難解答
圖片精選