const value = 'I am a student. My age is 5';
const regex = new RegExp('\\b' + value, 'i'); //what to add here?
console.log(regex.test('I am a student, my age is 5')); //true
console.log(regex.test('I am a student my age is 5')); //false
I want to ignore the ,
to make my second string to true.
you probably want a more generic regex like this:
const regex = /^I am a \w+[.,]? My age is \d+$/i
console.log(regex.test('I am a student, my age is 5')); //should be true
console.log(regex.test('I am a student my age is 5')); //should be true
console.log(regex.test('I am a INSERTWORD my age is 5')); //should be true
console.log(regex.test('I am a student my age is 230')); //should be true
a less genric regex could look like this:
const regex = /^I am a student[.,]? My age is 5$/i
console.log(regex.test('I am a student, my age is 5')); //should be true
console.log(regex.test('I am a student my age is 5')); //should be true
console.log(regex.test('I am a INSERTWORD my age is 5')); //should be false
console.log(regex.test('I am a student my age is 230')); //should be false
if you want to learn more about regexes go to one of:
https://regex101.com/ regex 101 is my go to regex testing website.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.