First, let me declare that I spent more than two hours getting regular expressions before writing this article. Miserable ~ Miserable ~ Miserable ~
According to the general idea, let's take a look at several other plug-in Methods: I use a string
Copy codeThe Code is as follows:
Var str = "eattd gebcat gedat jadu geat beu ";
For example.
1. The result starting with "ge" should be "gebcat, gedat, geat ". Because the word starts with "ge", I can put a new array for future use.
Copy codeThe Code is 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. The word ending with "at" returns "gebcat", "gedat", "geat ". Similarly, I can put it into an array.
Copy codeThe Code is as follows:
Var exp1 =/\ w + (at \ B)/ig;
3. for words that do not start with "ge", I need another array for storage.
Copy codeThe Code is 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". Well, the problem is. The Regex in Javascript is weak and does not support reverse-loop view negation. Therefore, it cannot be written as follows:
Copy codeThe Code is as follows:
Var exp1 =/\ w + (? <! At \ B)/ig;
While
Copy codeThe Code is as follows:
Var exp1 =/\ w + (?! At \ B)/ig;
\ B \ w is the word boundary. I will write it from another angle, find the word ending with at, and delete it from the original string. Add a new array.
Copy codeThe Code is 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!
You can't think about it.