简体   繁体   English

正则表达式匹配省略搜索中的第一个字符

[英]Regex match omitting first character in search

Haven't written JavaScript in a while so it's probably a very obvious mistake. 一段时间没有写JavaScript了,所以这可能是一个非常明显的错误。

using RegExp.match(..) in JavaScript, it's not picking up/ skipping the first character in the strings being searched for. 在JavaScript中使用RegExp.match(..),不会拾取/跳过要搜索的字符串中的第一个字符。 Below is the output from a list of strings: 以下是字符串列表的输出:

*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

So for example, 'Genetic engineering' should be: g,g,g but the first (,and only the first,) character is omitted. 因此,例如,“基因工程”应为:g,g,g,但第一个(也只有第一个)字符被省略。 If I typed 'enetic' for example then genetic engineering will match successfully. 例如,如果我键入“ enetic”,那么基因工程将成功匹配。

Here is the code: 这是代码:

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 );
           };
        }

This function returns a list of items for a search text field like so: 此函数返回搜索文本字段的项目列表,如下所示:

(8) ["Genetic engineering", "Human cloning", "Picotechnology", "Femtotechnology", "Grey goo", "Molecular engineering", "Molecular nanotechnology", "Nanoengineering"]

You have simple typo, missing the 'i' flag: 您有简单的错字,但缺少'i'标志:

let re = new RegExp(query, 'ig');

So your Regex only find lowercase letters. 因此,您的正则表达式只能找到小写字母。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM