Advanced text editing with Regular Expressions (Regex)
17/11/2024
Understand how to use regular expressions (regex) to efficiently split texts. Id: 20

Explanation - English
In this tutorial, we will learn how to split texts based on specific patterns using regular expressions in JavaScript. This allows us to perform a range of string manipulation operations, such as:
- Split by the pattern: Splits the text whenever the pattern appears anywhere in the line.
- Split immediately before the pattern: Splits the text immediately before the pattern.
- Create blocks before and after the pattern: Creates blocks of text before and after the pattern without removing it.
- Remove the pattern: Splits the text using the pattern and removes it from the blocks created.
Examples of Regular Expressions
Text: Alice: 21 years Bob: 35 years Charlie: 42 years, Daiane: 25 years
Regex: \d{2}
Break Type: Splits by the pattern if it appears anywhere in the line
Alice:yearsBob:yearsCharlie:years, Daiane:years
Text: Emails: - alice@example.com - bob@example.com - charlie123@example.com or charlie4@exemplo.br
Regex: [a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}
Break Type: Splits immediately before the pattern
Emails:-alice@example.com-bob@example.com-charlie123@example.com orcharlie4@exemplo.br
Text: Event dates: 2022-03-25 bla bla 2022-10-02 bla bla bla 2023-06-12 2024-08-05
Regex: \d{4}-\d{2}-\d{2}
Break Type: Creates blocks before and after the pattern (without removing the pattern)
Event dates:2022-03-25bla bla2022-10-02bla bla bla2023-06-122024-08-05
Text: Price list: Bla bla Product A: $50.00 bla bla bla bla Product B: $35.99 Product C: $12.75 bla bla Product D:$54,00
Regex: \$\d+\.\d{2}
Break Type: Uses the pattern as a divisor and removes it from the blocks
Price list:Bla bla Product A: bla bla bla blaProduct B:Product C: bla bla Product D:$54,00
Text: Codes: AB-123-CD EF-456-GH bla bla EF-456-AH bla 1F-456-GH IJ-789-KL
Regex: [A-Z]{2}-\d{3}-[A-Z]{2}
Break Type: Splits by the pattern if it appears anywhere in the line
Codes:bla blabla 1F-456-GH
Text: URLs: Bla bla https://site.com Bla http://example.com bla https://www.openai.com
Regex: https?://[\w.-]+
Break Type: Creates blocks before and after the pattern (without removing the pattern)
URLs:Bla blahttps://site.comBlahttp://example.comblahttps://www.openai.com