To find IPv4 addresses in a string using regex in Typescript, you can use the following regular expression:
index.ts392 chars5 lines
Explanation:
\b
: Matches a word boundary, ensuring that the regex only matches within words.(?:\d{1,3}\.){3}
: Matches three occurrences of one to three digits followed by a period, capturing them as a non-capturing group using (?:...)
.\d{1,3}
: Matches one to three digits.\b
: Matches a word boundary, ensuring that the regex only matches within words.g
: Enables global matching, allowing the regex to match multiple occurrences within the string.The text.match(regex)
method returns an array of all matches found in the string, which can then be used as needed.
gistlibby LogSnag