Regular Expression in Javascript

/^[A-Z]{6}\d{2}\D\d{2}\D\d{3}\D$/

1. What the hell is that?

Regular Expressions are as usefull as dangerous. If you are a programmer necessarily you will meet them. Over time, you’ll get better at it but they will remain too dangerous!

Sometimes the RegEx (or RE, technical term) may come in handy. But that doesn’t mean that we’ll use it at any time. Understand these and you will level up as programmer. As you undoubtedly realize, RegEx are not exclusive to the hottest language right now: Javascript.

But before figuring out RegEx, we must first understand why we need RegEx …

What are RegEx? OK, tell me if this sounds familiar: pulling needles out of haystacks… So, regular expressions helps you to do just that. These are the best tool to process and analyze strings.

Like everything in Javascript, RegEx are Object type. They may be called in two ways:

  1. Using the constructor RegExp
  2. Using slashes
let xyz= new RegExp(“xyz”);
let xyz = /xyz/;

One or the other means the same thing: this variable xyz is a regular expression 'xyz', nothing else. An example? Let’s go open a console panel in the browser, or just use node.js and copy the following line:

console.log(/xyz/.test(“Who am'I?”));
> False

Result: false. Try to guess it… well, /xyz/ is different than 'Who am'I'.
Try to turn it in to True, it’s fairly easy and fear not the test function, we’ll see it soon.

2. Functions out the wazoo

We shall explore only the test function, but remember that, this is just one of the many possibilities. In fact we have:

  • Test
  • Replace
  • Exec

Test accept as argument a string and return true if the latter is present or false if not. The syntax is: /regEx/.test("String to search for");
So let’s see how it goes with another example:

/alive/.test(“Look who's here, Ironman!”));
> False

Why it returns false? (Laughs) It’s a simple question, Ironman is dead. Let’s try again.

/dead/.test(“Ironman is dead?!”));
> True

Up until now it’s OK but what about this:

/^[A-Z]{6}\d{2}\D\d{2}\D\d{3}\D$/
.test("This is a nice try?");
> False!

Well, try to read it! First, let’s look at the opening slash (/) it tells us: here start the regex. After that, follows the symbol of openness (^), then, select any letter from A to Z and repeat it six times ([A-Z] {6}). Do the same with number from 0 to 9 (\d{2}), followed by only one letter (\D).
At this point, you can understand the rest.

3. Underneath the hood

The expression represents an italian tax code format.
For example ABCDEF12G34H567J. In fact, if we write this …

/^[A-Z]{6}\d{2}\D\d{2}\D\d{3}\D$/ .test("ABCDEF12G34H567J");
> True

Does a list of such symbols? Sure! I managed to steal it from Eloquent Javascript – I highly recommend this book!

/abc/		A sequence of characters
/[abc]/	Any character from a set of characters
/[^abc]/	Any character not in a set of characters
/[0-9]/	Any character in a range of characters
/x+/		One or more occurrences of the pattern x
/x+?/		One or more occurrences, nongreedy
/x*/		Zero or more occurrences
/x?/		Zero or one occurrence
/x{2,4}/	Between two and four occurrences
/(abc)/	A group
/a|b|c/	Any one of several patterns
/\d/		Any digit character
/\w/		An alphanumeric character (“word character”)
/\s/		Any whitespace character
/./			Any character except newlines
/\b/		A word boundary
/^/			Start of input
/$/			End of input

Further reading

My article in Italian on Medium
Eloquent Javascript
https://en.wikipedia.org/wiki/Regular_expression
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
https://www.w3schools.com/js/js_regexp.asp

Leave a Comment

Your email address will not be published. Required fields are marked *