字符 | 匹配 |
---|---|
\o | NUL字符 |
\t | 制表符 |
\n | 换行符 |
\v | 垂直制表符 |
\f | 换页符 |
\r | 回车 |
\xnn | 由十六进制nn指定的拉丁符,比如:\x0A等价于 |
\uxxxx | unicode字符 |
\cX | 控制字符^X |
正则表达式的js引用:var sEnd=new RegExp("s$"); ——以s为结尾的字符串匹配
function Verify() { //获取需验证的值,正则表达式 var InforString = document.getElementById("textInfor").value; var RuleString = document.getElementById("TextAreaRule").value; var inString = document.getElementById("textInfor").innerHTML; //正则表达式和需验证的值是否为空 if (InforString != "" && RuleString != "") { try { var reg = new RegExp(RuleString) var result = reg.test(InforString); if (result) { verifyResult.innerHTML = "通过!"; } else { verifyResult.innerHTML = "不通过!"; } } catch (e) { verifyResult.innerHTML = e.name + ":" + e.message; } } else if (InforString == "") { verifyResult.innerHTML = "需验证的字符为空!"; } else if (RuleString == "") { verifyResult.innerHTML = "正 则表达式为空!"; } } 正则表达式:
//问题:"A[B]C[D]E[F]G"将其分为两个数组,分别是 ACEG 和 [B][D][F].
function GroupString() {
var test = document.getElementById("textContent").value;
//方法一
// var test1 = test.match(/\[(\w)*\]/g);
//方法二
var s = /\[(\w)*\]/g;
var result;
var test1 = [];
while ((result = s.exec(test)) != null) {
test1.push(result[0]);
}
var test2 = test.split(/\[(\w)*\]/);
alert("数组一:" + test1 + "数组二:" + test2);
}
疑问:
都是寻找[B][D][F]数组的,不知哪个更好? //问题:"A[B]C[D]E[F]G"将其分为两个数组,分别是 ACEG 和 [B][D][F]. function GroupString() { var test = document.getElementById("textContent").value; //方法一 // var test1 = test.match(/\[(\w)*\]/g); //方法二 var s = /\[(\w)*\]/g; var result; var test1 = []; while ((result = s.exec(test)) != null) { test1.push(result[0]); } var test2 = test.split(/\[(\w)*\]/); alert("数组一:" + test1 + "数组二:" + test2); }
方法一:var test1 = test.match(/\[(\w)*\]/g);
方法二:var s = /\[(\w)*\]/g;
var result;
var test1 = [];
while ((result = s.exec(test)) != null) {
test1.push(result[0]);
}