'Using regular expressions to filter a given substring from a full string
Objective
The goal is to extract the substrings TEST_CASE
and RESULT
from the string | TEST_CASE | RESULT |
using regular expressions.
A possible solution
As an example [^\s*|]
selects only the first character.
Question
What are examples of regular expressions that would extract the required substrings?
Solution 1:[1]
I believe that this should do it: /[^\s\|]+/g
and use the g
global flag.
Solution 2:[2]
You should use a quantifier to repeat the character class one or more times like [^\s|]+
A negated character class matches not what you have listed in the character class and is kind of a broad match.
Another option is to be specific about what you do want to match. If you only want to match uppercase characters and an underscore, you could use [A-Z_]+
or match 1+ times a word character \w+
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | R.A. Lucas |
Solution 2 | The fourth bird |