regex Archives - Thoughts https://blog.jstassen.com/tag/regex/ A place for collecting my miscellaneous thoughts. Tue, 07 Dec 2021 07:01:10 +0000 en-US hourly 1 https://wordpress.org/?v=7.0.7 92836340 Exclude redirecting subdomains in htaccess RewriteRule https://blog.jstassen.com/2018/09/exclude-redirecting-subdomains-in-htaccess-rewriterule/ https://blog.jstassen.com/2018/09/exclude-redirecting-subdomains-in-htaccess-rewriterule/#respond Sun, 02 Sep 2018 04:26:45 +0000 http://blog.jstassen.com/?p=968 Redirecting websites to be secure with https can be a little tricky.  I had to exclude redirecting subdomains in htaccess RewriteRule.  This mostly because a number of my subdomain’s code lives within a subfolder of my primary domain. How to Exclude redirecting subdomains in htaccess RewriteRule To exclude redirecting subdomains in htaccess RewriteRule use the … Continue reading Exclude redirecting subdomains in htaccess RewriteRule →

The post Exclude redirecting subdomains in htaccess RewriteRule appeared first on Thoughts.

]]>
Redirecting websites to be secure with https can be a little tricky.  I had to exclude redirecting subdomains in htaccess RewriteRule.  This mostly because a number of my subdomain’s code lives within a subfolder of my primary domain.

How to Exclude redirecting subdomains in htaccess RewriteRule

To exclude redirecting subdomains in htaccess RewriteRule use the following RewriteCond. This tell Apache to only use the redirect if the request is to the top level domain, not a subdomain.

RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$

For instance, my .htaccess for jstassen.com looks like:

RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteRule ^(.*)$ https://jstassen.com/$1 [R=301,L]

This regex only fully matches the base domain. You can see this on Debuggex.

Exclude redirecting subdomains in htaccess RewriteRule

Note: websites with www are considered a subdomain. For instance www.jstassen.com is a subdomain of jstassen.com just like blog.jstasseen.com. This rule will therefore exclude the www subdomain. I have my www subdomain auto redirect to remove the www which I recommend.

The post Exclude redirecting subdomains in htaccess RewriteRule appeared first on Thoughts.

]]>
https://blog.jstassen.com/2018/09/exclude-redirecting-subdomains-in-htaccess-rewriterule/feed/ 0 968
Code: Regex for Instagram Username and Hashtags https://blog.jstassen.com/2016/03/code-regex-for-instagram-username-and-hashtags/ https://blog.jstassen.com/2016/03/code-regex-for-instagram-username-and-hashtags/#comments Mon, 14 Mar 2016 21:31:29 +0000 http://blog.jstassen.com/?p=716 Instagram is unique when it comes to the rules for usernames and hashtags. Here is a regex to capture and validating them with the JavaScript regex engine. The rules Two matches @ mentions with no space between @thebox193@discodude Matches with one . in them @disco.dude but not two .. @disco..dude Beginning period not matched @.discodude … Continue reading Code: Regex for Instagram Username and Hashtags →

The post Code: Regex for Instagram Username and Hashtags appeared first on Thoughts.

]]>
Instagram is unique when it comes to the rules for usernames and hashtags. Here is a regex to capture and validating them with the JavaScript regex engine.

The rules

  • Two matches @ mentions with no space between @thebox193@discodude
  • Matches with one . in them @disco.dude but not two .. @disco..dude
  • Beginning period not matched @.discodude
  • Ending period not matched @discodude.
  • Match underscores _ @_disco__dude_
  • Max characters of 30 @1234567890123456789012345678901234567890

The RegEx

(?:@)([A-Za-z0-9_](?:(?:[A-Za-z0-9_]|(?:\.(?!\.))){0,28}(?:[A-Za-z0-9_]))?)
Instagram username RegEx

Hashtags

For hashtags use the same regex, it’s the same rules, just replace the @ for a #.

(?:#)([A-Za-z0-9_](?:(?:[A-Za-z0-9_]|(?:\.(?!\.))){0,28}(?:[A-Za-z0-9_]))?)

Examples

You can try it out over on Debuggex. Here is a an example implemented in JavaScript.

The post Code: Regex for Instagram Username and Hashtags appeared first on Thoughts.

]]>
https://blog.jstassen.com/2016/03/code-regex-for-instagram-username-and-hashtags/feed/ 6 716