[英]regex (Javascript) allow comma to be ignore
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
我想忽略,
以使我的第二個字符串為真。
你可能想要一個更通用的正則表達式:
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/ regex 101 是我訪問 regex 測試網站的地方。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.