I spent more than two hours working on regular expressions before writing this article. I will introduce the specific implementation ideas for you. If you are interested, refer to the following. First, let me declare it, I spent more than two hours working on 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
The 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.
The 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 + ="
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.
The 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.
The 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 + ="
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:
The Code is as follows:
Var exp1 =/\ w + (?
While
The 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.
The 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 + ="
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 + ="
The result is: "+ wordsArr;
}
OK!
You can't think about it.