DEV Community

Cover image for Introduction to regex
Israel-Lopes
Israel-Lopes

Posted on • Updated on

Introduction to regex

What do regular expressions

Regular expression is a formal method of specifying a text pattern.

How so?

A brief explanation then!

Let's have a text, this text has more than a thousand words, you want to find
a specific word and not only that, it also wants to replace it whenever it occurs.

Regular expression can do this for you, locate the words and replace them according to a conditional logic established by you.

What are flags

Flags are markers that will determine how our regex will behave, they determine the logic of our regex, what will be filtered and what will be ignored.

Example:

  • flag (g) and the global flag, with it will be searched in the whole string.
  • flag(i), ignore case. Ignores uppercase and lowercase letters.

How to use the flags

 const text = 'Rato roeu a roupa do rei de Roma';

 const regex = /r/gi

 console.log(text.match(regex))
Enter fullscreen mode Exit fullscreen mode

output

 ['R', 'r', 'r', 'r', 'R']
Enter fullscreen mode Exit fullscreen mode

In this example above the flags "g" and "i" were used. Here it searches the entire string for uppercase or lowercase letters, noted in the output.

Checking for spaces

An example of fetching spaces with regex.

 console.log('   '.match(/\s/g))
Enter fullscreen mode Exit fullscreen mode

Single characters

const text = '1,2,3,4,5,6.a.b c!d?e'

const regex = /,/

console.log(text.split(regex))
Enter fullscreen mode Exit fullscreen mode

In this example it will separate our string where there is a comma and ignoring it. This result is only possible using split().

output:

['1', '2', '3', '4', '5', '6.a.b c!d?e']
Enter fullscreen mode Exit fullscreen mode

MetaCharacters

MetaCaracteres - Representatives

Metacharacters Name Meaning
. Point any one character (joker)
[] Set Allowed character set
[^] set denied Set of prohibited characters

MetaCharacters - Quantifiers

Metacharacters Name Meaning
? Optional zero or one
* Asterisk zero or more
+ Moist one or more
{n, m} Keys from n to m

MetaCharacters - Anchors

Metacharacters Name Meaning
^ Circumflex Inicio da linha
$ Cifrao Fim da linha
\b Borda Inicio ou fim da palavra

Other MetaCharacters

Metacharacters Name Meaning
\ Escape Using metacharacters with literals
\ or
() Group Defines a group
\1...\9 Rearview Rescue already defined groups

Using wildcard metacharacter:

const text = '1,2,3,4,,5,6,7,8,9,0'

console.logh(text.match(/1.2/g))
console.log(text.match(/1..2/g))
Enter fullscreen mode Exit fullscreen mode

output

['1,2']
null
Enter fullscreen mode Exit fullscreen mode

In this example above, it will search the entire string for a sequence that starts at "1" and ends at "2", but in the middle we have a wildcard, which means that it doesn't matter what character is there. In the second, it returns a null, because there are no two characters between 1 and 2.

Ways to search for space in string

In this example it will look for 3 spaces between "a" and "b". Below I made several ways that lead to the same result.

const text = 'a   b'

console.logh(text.match(/a   b/))
console.log(text.match(/a\s\s\sb/))
console.log(text.match(/a {3}b/))
console.log(text.match(/a\s{3}b/))
console.log(text.match(/\s+b/))
Enter fullscreen mode Exit fullscreen mode

Let's see other ways to make a regex.

Gets all letters in an A-Z range.

console.log(text.match(/[a-z]/g))
Enter fullscreen mode Exit fullscreen mode

You take all letters in a range of B-D.

console.log(text.match(/[b-d]/g))
Enter fullscreen mode Exit fullscreen mode

Gets all numbers in a range from 2 to 4.

console.log(text.match(/[2-4]/g))
Enter fullscreen mode Exit fullscreen mode

Gets all letters and numbers in the given range

console.log(text.match(/[A-Z1-3]/g))
Enter fullscreen mode Exit fullscreen mode

Texto alternativo da imagem)

Top comments (0)