Useful regular expressions that every developer should know

What is regular expression?

A regular expression is a pattern that is used to match character combinations in strings.

The regular expression is the most complicated thing if we don’t understand the basics of it. It is important to know the basics of it.

Throughout the software development career, it will be hard to find someone who never used regular expression.

But if we know the basics then it is really easy and smart things to do. Following are some really useful regular expression that we think every developer should know:

Regular expression examples:

The following code snippets are written in Java.

Replace various symbols

// Symbols that are not math symbols, currency signs, or combining characters.
inputString.replaceAll("\\p{So}+", "");

Replace all alphanumeric chanarcters.

inputString.replaceAll("^[a-zA-Z0-9]*$", "");
inputString.replaceAll("^\w+$", "");

The following code snippets are written in JavaScript.

Find the decimal part

const priceStr = '288.35 kr';
priceStr.replace(/\.\d+/, '');
// Output: 288.35 kr

Find email pattern

  const email = '';
  const isEamil = /^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(email);
  console.log(isEamil); // true

Match any character multiple times

// . is any char, * means repeated zero or more times.

Useful links for about regular expression: