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
我想忽略,
以使第二个字符串为true。
答案 0 :(得分:-1)
您可能想要这样的通用正则表达式:
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
不太通用的正则表达式可能看起来像这样:
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
如果您想了解更多有关正则表达式的信息,请访问以下其中一项:
https://regex101.com/正则表达式101是我去正则表达式测试的网站。