Home > front end >  Regular expression to find alphabetic characters between 2 strings
Regular expression to find alphabetic characters between 2 strings

Time:04-26

I need help constructing a regular expression used for our IT ticketing system to extract a location name.

Example test string: Project Name 377-New York Training Project

The string I want to extract is the location name: New York. So I need to keep the space in between, the location may or not have a space. "Project Name" and "Training Project" always stay the same and the number 377 will change depending on the location name.

This is what I am using:

(?<=Project Name)(.*)(?= Training Project)

The above code returns 377-New York so I just need to tweak it to remove the number and hyphen.

Thank you

CodePudding user response:

why not just add \d -

let subject = 'Project Name 377-New York Training Project'
let m = subject.match(/Project Name \d -(.*) Training Project/)
console.log(m[1])

CodePudding user response:

The first group will return to you only the city name:

(?:Project Name\s \d -)(.*?)(?:\s Training Project)
  • Related