Home > front end >  How to extract a pattern with Regex
How to extract a pattern with Regex

Time:02-01

I am trying to extract a pattern out of a sentence. For example , we have the following :

const randomVariable='I have read 200 regex books'
const notSoRandomVariable = 'I have practiced 9 regex test questions'

My purpose is to find the term that contains a number regex :

${number} regex

I have tried to do something like this :

const regexTrial1=/\b\d[\d,.]*\b/
const regexTrial2=/\b\d[\d,.]\s regex*\b/

But they do not work. Could you please help me understand how this would be solved? Thank you !

CodePudding user response:

const randomVariable='I have read 200 regex books'
const notSoRandomVariable = 'I have practiced 9 regex test questions'

console.log(randomVariable.match(/\d  regex/g));
console.log(notSoRandomVariable.match(/\d  regex/g));

CodePudding user response:

const regex = /[ -]?([0-9] ([.|,][0-9]*)?|[.][0-9] ) regex/g;

console.log('I have read 200 regex books'.match(regex));
console.log('I have read 200.0 regex books'.match(regex));
console.log('I have read 2,000 regex books'.match(regex));

  •  Tags:  
  • Related