HTML, JavaScript, Regular Expressions, TypeScript, Web Development

Regular Expression to get the Last Instance of a Word

The question of the day is how to use a regular expression to select the last instance of a word in a string of text. Finding the first instance is quite straightforward, but finding the last isn’t as intuitive.

So how’s it done?

The answer lies in a feature of the regular expression engine called Negative Lookahead.

Negative Lookahead is a regular expression lookaround function that belongs to a grouop of functions that do not select matching text. A negative lookahead lets you match a pattern that is then not followed by another pattern.

Let’s say we have an example string with several repeating instances of the text abc:

abcaaaaaaaabcbbbbabc111111abc22

If we want to match the last instance of the string abc, then we can use a negative lookahead that looks for the text abc, and that is not followed by any further abc text. In a regular expression this is written as:

abc(?!.*abc)

In this expression, the first abc is the pattern to match. Since there are many instances of abc in our example text, we will need a negative lookahead to help us only find the last instance of abc… in other words, the instance of abc where there are no following instances of abc. This is done using wildcard text matching: .* followed by the string abc.

As you can see, this is a straightforward and effective regular expression to match the last occurrence of a string of text.

Example in JavaScript

In a more practical example, let’s say we have an HTML text input area on our Web page as follows:

<input type="text" id="myText" value="the fox once said hello to the fox in the glen">

If we want to write a JavaScript function that will take the value of the text input area and change the meaning as an alert to the user, we could write a function to do so. Let’s say in this example that the fox said hello to a hawk instead of to another fox. To accomplish this replacement we will need to replace the last instance of the word fox in our string.

In JavaScript we can do so as follows:

var myTextValue = document.getElementById('myText').value;
var strOutText = 'initial: ' + myTextValue + '\r\n';
var strModText = strOutText.replace(/fox(?!.*fox)/, 'hawk');
strOutText += 'modified: ' +strModText;
alert(strOutText);

Example in TypeScript

I have been playing around with TypeScript and really enjoy the syntax. Notice that the main difference in the example below from the JavaScript in the example above is in assigning types to each variable… the string type is the only type that I use here, but if you are working with typescript you can use: string, number, bool, any, and null.

Here is the same example from the JavaScript above, written in TypeScript and outputting to a second text input rather than an alert:

Here is the HTML:

<input type="text" id="myText" value="the fox once said hello to the fox in the glen" style="width:300px;">
<br>
<input type="text" id="myTextOut" value="" style="width:300px;">

Here is the TypeScript version of the example:

var myTextValue: string = document.getElementById('myText').value;
var strOutText: string = myTextValue + '\r\n';
var strModText: string = strOutText.replace(/fox(?!.*fox)/, 'hawk');
strOutText += 'modified: ' +strModText;
document.getElementById('myTextOut').value = strModText;

 

Advertisement

3 thoughts on “Regular Expression to get the Last Instance of a Word”

Leave a Reply to Justin Cooney Cancel reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s