Home Web Front-end JS Tutorial Sample code for finding words that do not end with XX characters in Javascript_javascript tips

Sample code for finding words that do not end with XX characters in Javascript_javascript tips

May 16, 2016 pm 05:20 PM
javascript word Find

First, let me state that I spent over 2 hours playing with regular expressions before writing this article. Sad~sad~sad~

According to the general idea, let’s take a look at several other insertion methods: I use the string

Copy the code The code is as follows:

var str = "eattd gebcat gedat jadu geat beu";

is an example.

1. If it starts with "ge", the result should be "gebcat, gedat, geat". Since the word starts with "ge", I can put in a new array for later use.
Copy code The code is as follows:

var exp1 = /bgew /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. For words ending with "at", the result is "gebcat", "gedat", "geat". Likewise, I can put in an array.
Copy code The code is as follows:

var exp1 = /w (atb)/ig;

3. For words that do not start with "ge", I need another array to store them.
Copy code 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, here comes the problem. Regex in Javascript is relatively weak and does not support reverse lookaround negation, so it cannot be written:
Copy code The code is as follows:

var exp1 = /w (?

and
Copy code The code is as follows:

var exp1 = /w (?!atb)/ig;
The right side of the end of the word in the meaning of
cannot be " at", that is impossible, bw is to find word boundaries. I write it from another angle, find the word ending with at, and delete the word from the original string. Then put in a new array.
Copy code 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 atb/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, done!

Think and you will get; if you don’t think, you will not get.
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to implement an online speech recognition system using WebSocket and JavaScript How to implement an online speech recognition system using WebSocket and JavaScript Dec 17, 2023 pm 02:54 PM

How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

How to check the hard disk serial number and mac address How to check the hard disk serial number and mac address Feb 18, 2024 pm 07:45 PM

Hard drive serial numbers and MAC addresses are important identifiers in computer hardware and are very useful in managing and maintaining computer systems. This article will introduce how to find the hard disk serial number and MAC address. 1. Find the hard drive serial number. The hard drive serial number is a unique identifier used by the hard drive manufacturer to identify and track the hard drive. In different operating systems, the method of finding the hard drive serial number is slightly different. Windows: Open Command Prompt (search for "cmd" in the Start menu) and enter the following command and press Enter: wmicdisk

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems WebSocket and JavaScript: key technologies for implementing real-time monitoring systems Dec 17, 2023 pm 05:30 PM

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

4 Ways to Turn Off Find My on iPhone 4 Ways to Turn Off Find My on iPhone Feb 02, 2024 pm 04:15 PM

Apple's Find My app allows you to locate your iPhone or other device to prevent it from being lost or forgotten. While Find My is a useful tool for tracking devices, you may want to disable it if you're concerned about privacy issues, don't want to drain your battery, or for other reasons. Fortunately, there are several ways to turn off Find My on iPhone, all of which we will explain in this article. How to Turn off Find My on iPhone [4 Methods] You can turn off Find My on iPhone in four ways. If you used Method 1 to turn off Find, you can do this from the device you want to disable it on. To proceed with methods 2, 3, and 4, the iPhone that you want to turn off Find Finder should be powered off or

How to use JavaScript and WebSocket to implement a real-time online ordering system How to use JavaScript and WebSocket to implement a real-time online ordering system Dec 17, 2023 pm 12:09 PM

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

JavaScript and WebSocket: Building an efficient real-time weather forecasting system JavaScript and WebSocket: Building an efficient real-time weather forecasting system Dec 17, 2023 pm 05:13 PM

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

How to use Microsoft Reader Coach with Immersive Reader How to use Microsoft Reader Coach with Immersive Reader Mar 09, 2024 am 09:34 AM

In this article, we will show you how to use Microsoft Reading Coach in Immersive Reader on Windows PC. Reading guidance features help students or individuals practice reading and develop their literacy skills. You start by reading a passage or a document in a supported application, and based on this, your reading report is generated by the Reading Coach tool. The reading report shows your reading accuracy, the time it took you to read, the number of words correct per minute, and the words you found most challenging while reading. You will also be able to practice the words, which will help develop your reading skills in general. Currently, only Office or Microsoft365 (including OneNote for Web and Word for We

Simple JavaScript Tutorial: How to Get HTTP Status Code Simple JavaScript Tutorial: How to Get HTTP Status Code Jan 05, 2024 pm 06:08 PM

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

See all articles