Well, I’ve a Multiline Text like below. I want to extract the first Matched Content value (Here, 10
) after Messi:
using JavaScript Regular Expressions. The Matching String (Messi:
) Can be on any line.
Neymar: 11
Messi: 10
Ronaldo: 7
Chhetri: 11
Messi: 18
–Thanks In Advance.
3
Answers
You can use the following pattern.
The
$
will match the end of the line, so this will capture everything up to it.Example
This function finds the value of a key of choice:
Output:
Explanation of regex constructor:
'\b'
— word boundary+ key +
— dynamically concatenate key': ([^\n]*)'
— expect a colon, a space, and capture everything after that up to the next newlinewouldn’t it be better to parse this string once into some data-structure and then use that instead of messing around with regexes.