-1

I am trying to create a regex expression I can extract a name between a specific constant word and a period.

Test sentence:

Welcome Back Kumar. Your last login was April 10 2017.

Result: I want to extract the "Kumar" from the sentence.

I did create the following regex expression:

Back\s*(\w+)[^.]

And the result is as follow: "Back Kumar"

I can't find way to remove the "Back" word and the period at the end.

2 Answers2

0

If you want to look for a pattern that occurs after "Back\s*" without actually matching "Back\s*", you can use a positive lookbehind.

pattern = /(?<=^Welcome Back)\s*([\w\s]*)(?=\.)/;
results = "Welcome Back Kumar.".match(pattern);
console.log(results[1]); // "Kumar"

This solution will also match names with spaces, for example Welcome Back John Doe.

You can try it here: https://regex101.com/r/grhMk9/1

omijn
  • 646
  • 1
  • 4
  • 11
0

This regex should work.

/.+Back\s([^\s\.]+).+/

It basically captures only the content between the word "Back" and the first period afterwards.

Ariel
  • 341
  • 2
  • 5