SEO
RegEx for SEO
There are numerous advantages in incorporating RegEx for SEO and each of the following uses for RegEx have an advantage for SEO.
- RegEx in a text editor
- RegEx in web programming languages
- RegEx in operating systems
- RegEx in JavaScript
- RegEx in Google Analytics
- RegEx in Google Console
- Regex in HTML forms for validation
- Regex to analyse log files
After a brief intro to RegEx we will show examples using easy to notice RegEx Examples
An Intro to RegEx
The advantage of knowing basic RegEx techniques is they apply across many languages, devices and software, making it the ultimate productivity tool. Learn one set of logic and applying it in many applications. All of these have advantages for SEO. The faster and more efficiently you work, the more effective your SEO. And less headaches trying manually find errors in code and text.
Once you get used to the cryptic short cuts it becomes quite logical. A great way to learn is to use a text editor that supports RegEx in search replace. Like learning to program, learning RegEx is faster the quicker are your iterations between trial and error. Sometimes RegEx is applied to a string and not an entire block of text, so some examples may not work on every device.
Special Characters
RegEx | Matches | Example |
---|---|---|
^ |
The caret serves two different purposes. It is a special character that denotes "the beginning of a line" and it is a "not" operator inside of [ ]s |
[^a-z]
Any character that is not lowercase a-z. |
$ |
End of input or line |
\.$
In this case it is using the escape character in front of a . so it would find full-stops at the end of a line. |
* |
Matches zero or more times the preceding character(s). |
on*
In this case it would find "on" and "code". |
. |
Matches zero or more times the preceding character(s). |
an.
Matches "and" the phrase "branding is a business asset" but NOT an (if at the end of a line) |
\b |
Find a match at the beginning of a word |
\bPART
In this case it would find part but NOT depart. |
+ |
Matches one or more times the preceding character. |
t+
In this case it would find the t-shirt. |
n |
Preceding character is matched exactly n times. |
t{2}
In would find pattern and https but not it. |
| |
Either or |
seo|regex
In would find regex is good for seo. |
\d |
Matches digits |
\d{1,2}
In would find 1 and 12 |
RegEx in a text editor
Using an advanced text editor is a great way to learn RegEx. Learn techniques that allow you to create better blogs, which are a cornerstone of good SEO.
([\.\,\;\:\?\!])([a-zA-Z])
Finds possible punctuation errors. Note this is my go to check for a blog, it does also highlight correct code like a domain name www.example.com which should not be changed.
Note the () ()
indicate parameter 1 parameter 2 which you can use \1\2
in your replace to transfer the values found.
<.+>
Step through tags < > in HTML.
RegEx in web programming languages
Can check for reserved characters and whitespace. Automate tasks like creating image names based on product names - automatically removing special characters
File_Name = File_Name.replaceAll("\\W", "-");
The above Java code replaces all examples of \W : A non-word character with a - in a specific Java String called File_Name.
In Java we escape the escape character hence \\W.
RegEx in operating systems
Many operating systems incorporate RegEx. Especially Unix and Linux operating systems used on web servers.
Regex in JavaScript
var meString = "This is a string";
var newString = meString.replace(/i/g, "u");
newString now is "Thus us a strung" all "i"s replaced with "u".
The /g is a global flag on the regular expression to replace all occurrences.
RegEx in Google Analytics
RegEx is supported in Universal Analytics but not GA4. Universal Analytics is being faded out.
RegEx in Google Console
RegEx is available using Google Console Performance in Pages and Queries.
seo.*
This query loads all queries with clicks or impressions with the word seo.
With Shopping Carts the number of queries can be massive. As an SEO sorting through which products should be enhanced can be very fruitful - "picking the low hanging fruit".
Measuring the results of your SEO via Google Console makes sense - and doing that very quickly with RegEx makes even more sense.
Regex in HTML forms for validation
You have a shopping cart that is limited to Australian post codes.
^\d{4}
OR
^[0-9]{4}
Full form input-:
<input id="Post_Code" pattern="^[0-9]{4}" name="Post_Code" type="text" placeholder="4217" value="" maxlength="4" required="">
Regex to analyse log files
There are many programs to analyse log files and put that information into a meaningful format. However sometimes you may want to know something very specific. And you may want that information quickly, without downloading a new program or app.
Regex can very quickly remove all but the information you are NOT looking for in seconds. Thus leaving the information you are looking for - so instead of 10,000 lines you may have 100. We may want to know information about Google Crawl Stats for a specific site. Paste the lines into a text editor (that supports Regex) - a few simple expressions later we have the information we are after.
Regex origins
RegEx was originally developed by mathematician Stephen Cole Kleene in an attempt to describe early artificial neural networks (1951).
A Gold Coast SEO and Web Developer