Sample Code for finding words that do not end with XX characters in Javascript _ javascript tips-js tutorial

Source: Internet
Author: User
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.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.