Загрузка...

Extracting Number/Number/Number Patterns from Strings with JavaScript Regex

Learn how to efficiently extract number patterns separated by slashes (e.g., '8/8/69') from text using concise JavaScript regular expressions.
---
This video is based on the question https://stackoverflow.com/q/79481727/ asked by the user 'soroosh azadi' ( https://stackoverflow.com/u/21052318/ ) and on the answer https://stackoverflow.com/a/79481743/ provided by the user 'Guillaume Outters' ( https://stackoverflow.com/u/1346819/ ) at 'Stack Overflow' website. Thanks to these great users and Stackexchange community for their contributions.

Visit these links for original content and any more details, such as alternate solutions, latest updates/developments on topic, comments, revision history etc. For example, the original title of the Question was: Get Number/Number/Number structure in the given string

Also, Content (except music) licensed under CC BY-SA https://meta.stackexchange.com/help/licensing
The original Question post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/by-sa/4.0/ ) license, and the original Answer post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/by-sa/4.0/ ) license.

If anything seems off to you, please feel free to drop me a comment under this video.
---
Problem Overview

You have a string containing text and special sequences formatted as numbers separated by slashes, such as 8/8/69. Your goal is to extract only those sequences that follow a Number/Number/Number pattern, allowing for optional spaces around numbers and slashes.

For example:

[[See Video to Reveal this Text or Code Snippet]]

You want to extract something like:

[[See Video to Reveal this Text or Code Snippet]]

Note that the numbers and slashes might be spaced irregularly, such as ' 8/ 8 / 69 ' or compacted like '8/8/69'.

Solution Using JavaScript Regex

Regular expressions provide an elegant way to match this pattern directly rather than splitting and validating manually.

The Regex Pattern

[[See Video to Reveal this Text or Code Snippet]]

What this means:

[0-9]+ : One or more digits

*: Zero or more spaces

/: A literal slash / (escaped because slashes delimit the regex)

This matches three groups of numbers separated by slashes with optional spaces, for example:

8/8/69

8 / 8 / 69

JavaScript example:

[[See Video to Reveal this Text or Code Snippet]]

The g flag makes sure all matches in the string are returned.

Extending to more numbers

If you want to match sequences like 1/2/3/4 or a flexible number of numbers separated by slashes, you can use repetition:

[[See Video to Reveal this Text or Code Snippet]]

This uses:

( */ *[0-9]+ ){n}: exactly n occurrences of "slash + optional spaces + number"

Summary

Use [0-9]+ */ *[0-9]+ */ *[0-9]+ to match three numbers separated by slashes,

The pattern accommodates spaces before/after the slashes,

Use repetition if you want variable numbers of groups,

Leverage JavaScript's String.match() with the g flag to find all occurrences.

This approach is concise, reliable, and easy to adapt to different requirements around the number of groups or spacing.

Видео Extracting Number/Number/Number Patterns from Strings with JavaScript Regex канала vlogommentary
Яндекс.Метрика
Все заметки Новая заметка Страницу в заметки
Страницу в закладки Мои закладки
На информационно-развлекательном портале SALDA.WS применяются cookie-файлы. Нажимая кнопку Принять, вы подтверждаете свое согласие на их использование.
О CookiesНапомнить позжеПринять