[英]Regex match omitting first character in search
一段時間沒有寫JavaScript了,所以這可能是一個非常明顯的錯誤。
在JavaScript中使用RegExp.match(..),不會拾取/跳過要搜索的字符串中的第一個字符。 以下是字符串列表的輸出:
*query = 'g'*
Genetic engineering: g,g
Gene therapy: null
Surfactant: null
Human cloning: g
Protein production: null
Microfluidics: null
Polymerase chain reaction: null
RNA: null
Restriction enzyme: null
Picotechnology: g
Femtotechnology: g
Grey goo: g
Molecular engineering: g,g
Microfluidics: null
Molecular nanotechnology: g
Nanoengineering: g,g
Atom probe: null
Maxwell's demon: null
因此,例如,“基因工程”應為:g,g,g,但第一個(也只有第一個)字符被省略。 例如,如果我鍵入“ enetic”,那么基因工程將成功匹配。
這是代碼:
function createFilterFor(query) {
return function filterFn(state) {
let re = new RegExp(query, 'g');
console.log(state+":\t"+state.match(re,'ig'))
return (state.match(re,'ig') != null? true : false );
};
}
此函數返回搜索文本字段的項目列表,如下所示:
(8) ["Genetic engineering", "Human cloning", "Picotechnology", "Femtotechnology", "Grey goo", "Molecular engineering", "Molecular nanotechnology", "Nanoengineering"]
您有簡單的錯字,但缺少'i'標志:
let re = new RegExp(query, 'ig');
因此,您的正則表達式只能找到小寫字母。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.