Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex select word after specific word

How can I capture a word just after specific word in regex, I have to select everything between from - to and after to so there will be two capturing groups.

Example: "From London to Saint Petersburg" I wanted to extract London Saint Petersburg from above string.

Im stuck with this code here, my current regex selecting to Saint Petersburg i wanted to get rid word from and to from the selection.

/(?=to)(.*)/i
like image 565
Wimal Weerawansa Avatar asked Dec 23 '22 14:12

Wimal Weerawansa


1 Answers

You can capture the two groups you need and then use match to extract them:

s = "From London to Saint Petersburg"

console.log(
  s.match(/From (.*?) to (.*)/).slice(1,3)
)
like image 199
Psidom Avatar answered Dec 28 '22 09:12

Psidom