首先,让我声明一下,我在写这篇文章之前花了2个多小时在弄正则表达式。悲~悲~悲~
按照一般的思路,先来看看其他几个插找方式: 我以字符串
复制代码 代码如下:
var str = "eattd gebcat gedat jadu geat beu";
复制代码 代码如下:
var exp1 = //bge/w+/ig;
var matchedStr = exp1.exec(str);
while (matchedStr != null && matchedStr.index < str.length) {
if (matchedStr[0] != null) {
inv.innerHTML += "<br>The result is: " + matchedStr[0];
//newStr = newStr.replace(matchedStr[0]);
wordsArr.push(matchedStr[0]);
}
matchedStr = exp1.exec(str);
}
复制代码 代码如下:
var exp1 = //w+(at/b)/ig;
复制代码 代码如下:
var exp1 = //b(?!ge)/w+/ig;
var wordsArr = new Array();
var matchedStr = exp1.exec(str);
while (matchedStr != null && matchedStr.index < str.length) {
if (matchedStr[0] != null) {
inv.innerHTML += "<br>The result is: " + matchedStr[0];
newStr = newStr.replace(matchedStr[0]);
wordsArr.push(matchedStr[0]);
}
matchedStr = exp1.exec(str);
}
//wordsArr = newStr.split(" ");
for (var i = 0; i < wordsArr.length;) {
if (wordsArr[i] == "undefined") {
wordsArr.splice(i,1);
} else
i++
}
复制代码 代码如下:
var exp1 = //w+(?<!at/b)/ig;
复制代码 代码如下:
var exp1 = //w+(?!at/b)/ig;
复制代码 代码如下:
function RegularExpTest() {
var inv = document.getElementById("RegexTest");
var str = "eattd gedbcat gedat jadu geat beu";
var newStr = str;
var exp1 = //w+at/b/ig;
var wordsArr = new Array();
var matchedStr = exp1.exec(str);
while (matchedStr != null && matchedStr.index < str.length) {
if (matchedStr[0] != null) {
inv.innerHTML += "<br>The result is: " + matchedStr[0];
newStr = newStr.replace(matchedStr[0]);
}
matchedStr = exp1.exec(str);
}
wordsArr = newStr.split(" ");
for (var i = 0; i < wordsArr.length;) {
if (wordsArr[i] == "undefined") {
wordsArr.splice(i,1);
} else
i++
}
inv.innerHTML += "<br>The result is: " + wordsArr;
}
新闻热点
疑难解答
图片精选