Days
:
Hours
:
Minutes
:
Seconds

Best Consulting WordPress Theme $69 $29

View Now
Skip to content Skip to sidebar Skip to footer

An Introduction to Regular Expressions for SEO

If you’re not a software developer, you have probably never heard of regular expressions. Regular expressions (regex) are tools you can use to filter or search large amounts of data without programming. With regular expressions for SEO, you can find hidden gems within your Google Analytics, filter spammers in your website log files, or perform advanced configuration on your web server.

Don’t wait any minute and dive in these amazing SEO tips kindly provided by our fellow writer Brian Johnson from Siteturner.com.

What Are Regular Expressions for SEO?

Regular expressions are code snippets that define a complex search query. Here’s an example of a regular expression:

/\s+\[(\d+)\]\s*/gi

Whoa! Looks complicated, huh?  Actually, they’re not so bad, once you understand the basics.  You can use regular expressions for SEO in a lot of different pieces of software, from Google Analytics and Tag Manager to Notepad++. Since the syntax is the same anywhere, regex is incredibly powerful – once you learn to use it.

Getting Started With Regular Expressions for SEO

regex-seo-social-card
The first thing to do in order to learn regular expressions is to open a regex testing tool. Our favorite is regex101.com. It not only allows you to test regexes, but it also explains how it is parsing them. You can use it to try out the following examples. The basic format of a regular expression is this:

/<query>/<params>

The <query> is the query you want to perform, and params are the parameters that you want to use to perform the query. We will mostly ignore params for this article, aside from one: i. If you use the <param> i, your query will be non-case-sensitive.

How do you write a query? Well, let’s start with something simple: Let’s say you want to search for the word “cat” using a regex. That’s pretty simple – all you have to do is write it as the query:

/cat/i

When you put that query into your regex program, it will match the word “cat” in your document, log, or whatever. 

Now, let’s say we want to instead search for something more useful. Perhaps you have a log file, and you want to search for any hits from Google. For this query, you will use a few new regex commands: . and *. The . command matches any character, and the * matches 0 or more of whatever is before it. So to search for <anything>.google.com, you would use this command:

/.*\.google\.com/i

The first. in the query says “I want to match any character,” and the * after it says “I want to match any character 0 or more times.” The rest of the query, \.google\.com, matches “.google.com.”  

The backslash (\) character is an escape character, telling the parser to take the following character as is, instead of as a command. If we hadn’t put the backslash before the dots in “.google.com”, the program would have interpreted them as the “match any character” command.

There are many, many more commands that you can use for regexes. You can check out an entire list of commands here.

How to Use Regular Expressions for SEO

SEO

So where can you use regular expressions for SEO? There are dozens of places. Below are just a few of the most useful.  

  • Google Analytics allows you to use regex for filters. For example, you can filter traffic by IP address. Use a query like /18\.242\.\d*\.\d*/ to search for IP addresses of the form 18.242.x.x. Note that the \d command matches any digit, so the \d* command matches any multi-digit string.
  • Notepad++ or other text editors can use regex to perform searches. For example, you may want to search log files using the commands in the previous section.
  • Configuration files are another place in which you can use regex. Most web servers, such as Apache or Nginx, accept regex string in many configuration functions. Check out your web server documentation for specifics.
  • Keyword query tools and other SEO tools often support regex. Using these, you can search for keywords in a much more efficient manner than by manually going through a bunch of searches.

Conclusion

We know that this is a lot to take in, and what we’ve offered is a very basic overview of regular expressions and their use in SEO. Hopefully, you can use these basics and the links provided in order to get started in using these wonderful tools.

For the Updates

Exploring ideas at the intersection of design, code, and technology. Subscribe to our newsletter and always be aware of all the latest updates.

Leave a comment

Download a Free Theme