First, let me make a statement that I spent 2 hours working on regular expressions before writing this article. Sad ~ sad ~ sad ~
Follow the general idea, first look at the other several interpolation ways: I am a string
Copy Code code as follows:
var str = "Eattd gebcat gedat jadu geat Beu";
As an example.
1. At the beginning of "GE", the result should be "Gebcat, Gedat, geat". Because the word starts with "GE", I can put a new array in for later use.
Copy Code code as follows:
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);
}
2. Words ending with "at" result in "Gebcat", "Gedat", "geat". Again, I can put in an array.
Copy Code code as follows:
var exp1 =/\w+ (at\b)/ig;
3. Words that don't start with "GE", I need another array to store.
Copy Code code as follows:
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++
}
4. Words that do not end with "at", OK, here's the problem. The regex in JavaScript is weaker and does not support reverse-scan negation, so it cannot be written:
Copy Code code as follows:
var exp1 =/\w+ (? <!at\b)/ig;
and
Copy Code code as follows:
var exp1 =/\w+ (?!) at\b)/ig;
The right of the end of the word can not be "at", that is not possible, \b\w is to find the word boundary. I'll write it in a different way, find the at end and remove the word from the original string. Then put in a new array.
Copy Code code as follows:
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;
}
OK, it's done!
To think is to be, but not to think.