How to Use REGEXREPLACE Function in Google Sheets

REGEXREPLACE is a handy function offered by Google Sheets that lets you quickly replace parts of strings in your sheets.

It comes as part of Google Sheets’ suite of REGEX functions along with functions like REGEXEXTRACT and REGEXMATCH.

At first, the inclusion of regular expressions in REGEX functions might seem intimidating, but once you get the hang of it, you will find yourself frequently using them in your spreadsheets.

What does the REGEXREPLACE Function Do?

The REGEX function simply replaces part of string that matches a particular pattern and replaces it with a different text.

The matching in the REGEXREPLACE function is usually done with regular expressions.

Syntax of the REGEXREPLACE Function

The syntax for the REGEXREPLACE function is as follows:

REGEXMATCH(text, reg_exp, replace_text)

Here,

  • text is the text or string, a part of which needs to be replaced.
  • reg_exp is a regular expression. The expression matches parts of the text that need to be replaced.
  • replace_text is the text that will replace all the parts of text that match the reg_exp.

Applications of the REGEXREPLACE Function

The REGEXREPLACE function can be quite helpful when you want to replace a particular search string or pattern in multiple cells.

Here are some useful applications of the REGEXREPLACE function:

  • You can use it to replace or remove a particular letter, word or phrase in different cells.
  • You can use it to remove or replace all spaces from a text
  • You can use it to remove or replace all numerical values in a string
  • You can use it to remove or replace all URLS from a string
  • You can use it to remove html tags from a string

There are many other ways in which you can apply the REGEXREPLACE function.

Once you start getting comfortable with REGEXREPLACE and regular expressions, you will find that the possibilities are endless.

Let us see how REGEXREPLACE can be used in each of the above applications

Using the REGEXREPLACE Function to Replace or Remove a Letter, Word or Phrase in a String

Let’s first see how you can use REGEXREPLACE to remove a specific letter, word, or phrase from selected cells.

REGEXREPLACE can be quite handy if you want to remove the hash symbol from hashtags in content that had been scraped from social media.

Let us say you have the following text in cell A2 and want to remove all instances of the ‘#’ character from it.

Original Text with hash symbol

For this, you can use the REGEXREPLACE function as follows:

=REGEXREPLACE(A2,"#", "")

Here’s the result you will get:

RegexReplace to Remove Hash from Text

Similarly, if you want to replace all instances of a word in a cell with another word, you can use the REGEXREPLACE function.

For example, let us say you want to replace all instances of the word ‘Moscow’ with the word ‘Paris’ in cell A2.

Original Text where we need to replace a word

Here’s the formula you can use:

=REGEXREPLACE(A2,"Moscow", "Paris")

And here’s the result you will get:

RegexReplace to replace a word

You can also use REGEXREPLACE to remove or replace any occurrence of more than one word in a cell. For example, say you have the following list of web URLs in cell A2:

Data where multiple words need to be removed

Let’s say you want to remove all occurrences of the words ‘.com’, ‘.net’,’.edu’ or ‘.io’ from the contents of cell A2. In that case, you can use the REGEXREPLACE function as follows:

=REGEXREPLACE(A2,".com|.net|.edu|.io","")

Here’s the result you will get:

Data where multiple words need to be removed

Using the REGEXREPLACE Function to Remove all Spaces from a String

A common application of the REGEXREPLACE function is to remove spaces from text in a cell.

Let us say you have the following text in cell A2 and you want to remove all the spaces in it to convert it into one big word:

Text with spaces

Since space is nothing but a simple character, you can use the REGEXREPLACE function just as we did in the first example:

=REGEXREPLACE(A2," ", "")

Here’s the result you will get:

RegexReplace formula to remove all spaces

You could also replace the spaces with another character, for example, a comma:

=REGEXREPLACE(A2," ", ",")

Here’s the result you will get in that case:

Replace space with comma

Using the REGEXREPLACE Function to Remove or Replace all Numerical values in a String

If you want to replace or remove all numbers from a cell, you need to use the “[0-9]” regex characters.

The square brackets are used to hold a set of characters. Since you want to match any number between 0 and 9, we use the 0-9 regular expression inside the square brackets.

This means ‘match any character that is between 0 and 9’.

Let us say you have the following text in cell A2 and you want to remove all numbers from it:

Text with Numbers

In that case, your formula should be:

=REGEXREPLACE(A2, "[0-9]","")

Here’s the result you should get:

Formula to remove numbers from string

If you also want to ensure that all numbers, including those with decimal points, get removed, then you can use the following formula:

=REGEXREPLACE(A2, "[0-9]*\.[0-9]|[0-9]","")

Here, we used the ‘.’ character preceded by the escape character ‘\’ so that the ‘.’ character is not mistaken for a regular expression.

The ‘*’ character represents zero or more occurrences of a character or string, while the ‘+’ character represents at least one or more occurrences of a character or string.

This ensures that the regular expression matches even numbers that don’t have any digits before the decimal point.

After the ‘|’ operator we added another [0-9] expression because we also want to consider cases where the number is an integer (with no decimal point at all).

Here’s the result you should get:

Formula to remove numbers from string when there is decimal

Using the REGEXREPLACE Function to Remove or Replace Web URLs from a String

There may be cases where you want to remove all web URLs from a cell. You can do this by using the (.*) expression.

This expression represents any number of characters. When we place this expression between the words ‘www’ and ‘com’, the expression represents any string that starts with www and ends with com.

So to remove all instances of a web URL from cell A2, you can use the formula:

=REGEXREPLACE(A2, "www(.*)com","")

Alternatively, you can also use the following formula:

=REGEXREPLACE(A2, "^www\.[a-zA-Z\.]+com","")

Formula to remove website URL

Using the REGEXREPLACE Function to Remove HTML Tags from a String

A common use of the REGEXREPLACE function is to remove special characters around a word.

For example, say you have copied some markup text from a website and need to remove all the HTML tags from it.

Text with special characters

REGEXREPLACE makes this really easy. Here’s the formula you can use:

=REGEXREPLACE(A2, "(\<([A-Za-z1-9]+)\>)|(\</([A-Za-z1-9]+)\>)","")

The above formula might look a little complex, but if you break it down, you’ll see it’s actually quite simple. The regular expression in the above formula can mainly be divided into two parts:

  • One part deals with instances where you have opening tags, like <p> and <code>
  • One part deals with instances where you have closing tags, like </p> and </code>

For the opening tags, we used the regular expression: “(\<([A-Za-z1-9]+)\>)”. This means match all instances that start with the ‘<’ symbol and end with the ‘>’ symbol.

We added an escape character before the two symbols to ensure that they are not mistaken for regular expression symbols.

We also specified that in between these two symbols there could be alphabets in lower or uppercase as well as numbers from 1-9 (if you consider h1, h2,… tags).

For the closing tags, we used the regular expression: “(\</([A-Za-z1-9]+)\>)”. This means match all instances that start with the ‘</’ symbols and end with the ‘>’ symbol.

We also specified that in between these two symbols there could be alphabets in lower or uppercase as well as numbers from 1-9.

The two expressions have been separated with the ‘|’ symbol so that the regular expression matches any of the two sub-expressions.

Here’s the result you should get when you apply the above formula to the text:

Formula to replace special characters

Points to Remember

Here are a few important points that you need to remember when using the REGEXREPLACE function:

  • The REGEXREPLACE function only works with text input. It does not work with numbers. If you want to use numbers as input (for example telephone numbers), you need to first convert it to text, using the TEXT function.
  • The function is case-sensitive. Therefore, you will need to specify the correct case inside the regular expression. You may also convert the entire input string to upper or lower case using the UPPER or LOWER functions.
  • The REGEXREPLACE function can have numerous applications, once you learn how to use it effectively. We have tried to show you some of these applications in this tutorial.

In order to use this function, good knowledge of regular expressions helps, and the best way to get a good hold over it is to practice.

Play around with different regular expressions and see what results you get.

You will be surprised by how useful the REGEXREPLACE function can be once you start using it in your day-to-day applications.

Spreadsheet Expert at Productivity Spot | Website | + posts

Google Sheets and Microsoft Excel Expert.