Regular expressions verified

Quick reference about regular expressions (REGEX) in Salesforce Marketing Cloud (SFMC) with SSJS (server-side JavaScript).

Table of contents

URL

var url = "https://www.ampscript.xyz";
var url_match = /^(http[s]?:\/\/?[^:\/\s]+[^\/]*?\/\w+\.)*([^#?\s]+)(\?([^#]*))?(#(.*))?$/g;
var match = url.match(url_match);

Write(Stringify(match));
<script runat="server">

    Platform.Load("core", "1");

	try {

        var url = "https://www.ampscript.xyz";
        var url_match = /^(http[s]?:\/\/?[^:\/\s]+[^\/]*?\/\w+\.)*([^#?\s]+)(\?([^#]*))?(#(.*))?$/g;
        var match = url.match(url_match);

        Write(Stringify(match));
		
	} catch(error) {
        Write(Stringify(error));
    }	

</script>
[
  "https://www.ampscript.xyz",
  "",
  "https://www.ampscript.xyz",
  "",
  "",
  "",
  ""
]

Email address

var email = "example@mail.com";
var email_match = /^([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,})$/g;
var match = email.match(email_match);

Write(Stringify(match));
<script runat="server">

    Platform.Load("core", "1");

	try {

        var email = "example@mail.com";
        var email_match = /^([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,})$/g;
        var match = email.match(email_match);

        Write(Stringify(match));
		
	} catch(error) {
        Write(Stringify(error));
    }	

</script>
[
  "example@mail.com",
  "example",
  "mail",
  "com"
]

Phone number (Belgium)

var phone = "0032493112233";
var phone_match = /^(\+[0-9]{2}[.\-\s]?|00[.\-\s]?[0-9]{2}|0)([0-9]{1,3}[.\-\s]?(?:[0-9]{2}[.\-\s]?){4})$/g;
var match = phone.match(phone_match);

Write(Stringify(match));
<script runat="server">

    Platform.Load("core", "1");

	try {

        var phone = "0032493112233";
        var phone_match = /^(\+[0-9]{2}[.\-\s]?|00[.\-\s]?[0-9]{2}|0)([0-9]{1,3}[.\-\s]?(?:[0-9]{2}[.\-\s]?){4})$/g;
        var match = phone.match(phone_match);

        Write(Stringify(match));
		
	} catch(error) {
        Write(Stringify(error));
    }	

</script>
[
  "0032493112233",
  "0032",
  "493112233"
]

VAT (Belgium)

var vat = "BE1234567890";
var vat_match = /^([A-Z]{2})([0-9A-Z]{8,12})$/g;
var match = vat.match(vat_match);

Write(Stringify(match));
<script runat="server">

    Platform.Load("core", "1");

	try {

        var vat = "BE1234567890";
        var vat_match = /^([A-Z]{2})([0-9A-Z]{8,12})$/g;
        var match = vat.match(vat_match);

        Write(Stringify(match));
		
	} catch(error) {
        Write(Stringify(error));
    }	

</script>
[
  "BE1234567890",
  "BE",
  "1234567890"
]

IBAN

var iban = "BE68539007547034";
var iban_match = /^([A-Z]{2})(\d{2})([A-Z\d]+)$/g;
var match = iban.match(iban_match);

Write(Stringify(match));
<script runat="server">

    Platform.Load("core", "1");

	try {

        var iban = "BE68539007547034";
        var iban_match = /^([A-Z]{2})(\d{2})([A-Z\d]+)$/g;
        var match = iban.match(iban_match);

        Write(Stringify(match));
		
	} catch(error) {
        Write(Stringify(error));
    }	

</script>
[
  "BE68539007547034",
  "BE",
  "68",
  "539007547034"
]

GUID

var guid = "C4729C50-BAAF-4C7D-A7E3-476773C714EB";
var guid_match = /^[0-9a-zA-Z]{8}\-[0-9a-zA-Z]{4}\-[0-9a-zA-Z]{4}\-[0-9a-zA-Z]{4}\-[0-9a-zA-Z]{12}$/g;
var match = guid.match(guid_match);

Write(Stringify(match));
<script runat="server">

    Platform.Load("core", "1");

	try {

        var guid = "C4729C50-BAAF-4C7D-A7E3-476773C714EB";
        var guid_match = /^[0-9a-zA-Z]{8}\-[0-9a-zA-Z]{4}\-[0-9a-zA-Z]{4}\-[0-9a-zA-Z]{4}\-[0-9a-zA-Z]{12}$/g;
        var match = guid.match(guid_match);

        Write(Stringify(match));
		
	} catch(error) {
        Write(Stringify(error));
    }	

</script>
[
  "C4729C50-BAAF-4C7D-A7E3-476773C714EB"
]

Journey Builder Automation

var jb_auto = "Auto_Send 1984-01-29T0000.999";
var jb_auto_match = /(?:(\d{1,4}\-\d{1,2}\-\d{1,2}T\d{1,6}\.\d{3}))/g;
var match = jb_auto.match(jb_auto_match);

Write(Stringify(match));
<script runat="server">

    Platform.Load("core", "1");

	try {

        var jb_auto = "Auto_Send 1984-01-21T0000.999";
        var jb_auto_match = /(?:(\d{1,4}\-\d{1,2}\-\d{1,2}T\d{1,6}\.\d{3}))/g;
        var match = jb_auto.match(jb_auto_match);

        Write(Stringify(match));
		
	} catch(error) {
        Write(Stringify(error));
    }	

</script>
[
  "1984-01-21T0000.999",
  "1984-01-21T0000.999"
]

Journey Builder Triggered Send Definition

var jb_tsd = "Newsletter_201912 - 3423f3a484214dee92cf3652903751f2";
var jb_tsd_match = /(\s\-\s[0-9a-z]{32,50})$/g;
var match = jb_tsd.match(jb_tsd_match);

Write(Stringify(match));
<script runat="server">

    Platform.Load("core", "1");

	try {

        var jb_tsd = "Newsletter_201912 - 3423f3a484214dee92cf3652903751f2";
        var jb_tsd_match = /(\s\-\s[0-9a-z]{32,50})$/g;
        var match = jb_tsd.match(jb_tsd_match);

        Write(Stringify(match));
		
	} catch(error) {
        Write(Stringify(error));
    }	

</script>
[
  " - 3423f3a484214dee92cf3652903751f2",
  " - 3423f3a484214dee92cf3652903751f2"
]

Last Updated: