JavaScript find Word sample code that does not end with XX characters _javascript tips

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