Sujit Singh
Sujit Singh

Reputation: 88

regex filter text match in react giving null

I am trying to get the Project Name from record description like below

Project Name:[xyz 28912] omiture

Here's my code

record.description.forEach(tempDescription => {
 var projectTitle = tempDescription.text.match(/Project Name:.*?(?=\s+Following)/gs);
 console.log(projectTitle); // giving null    if(null !== projectTitle    &&  undefined !==projectTitle){    var titleArr = projectTitle[0].split(":");
 console.log(titleArr)
} })

I need help on it

Upvotes: 0

Views: 130

Answers (1)

mplungjan
mplungjan

Reputation: 177960

Do you mean

https://regex101.com/r/v5NoQ1/1

/Project Name:\[(.*?)\]\s+(.*)/gs

as in

const record = {
  description: [
  { text: "Project Name:[xyz 28911] omiture" },
  { text: "Project Name:[xyz 28912] omiture" },
  { text: "Project Name:[xyz 28913] omiture" }
  ]
};

const re = /Project Name:\[(.*?)\]\s+(.*)/gs;
const projectNames = record.description.map(tempDescription => {
  const [a] = tempDescription.text.matchAll(re);
  return {
    name: a[1],
    desc: a[2]
  }
})
console.log(projectNames)

Upvotes: 1

Related Questions