Searching with Regex, excluding the initial character from the match

It's been a while since I last wrote JavaScript, so any mistakes are probably very obvious.

I'm currently facing an issue with using RegExp.match(..) in JavaScript. It seems to be skipping the first character in the strings that are being searched for. Here 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

For example, 'Genetic engineering' should match as: g,g,g but the first (and only the first) character is missing. If I type 'enetic', 'genetic engineering' will successfully match.

Here is the code snippet causing the issue:

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 is designed to provide a list of items for a search text field. Here are some of the results:

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

Answer №1

The error in your code is a simple typo - you are missing the 'i' flag:

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

As a result, your Regex only matches lowercase letters.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Bringing in data from <script> to use in <script setup>

To ensure unique ids for instances of a Vue component, I am using a simple generator to enumerate them. Typically, I would initialize the generator outside of the setup function like this: const idGen = generateIds("component:my-component"); export defaul ...

How to incorporate JSON into a d3.js calendar display?

Learning d3 charts and javascript has been quite challenging for me. After researching a lot, I successfully populated the chart with CSV data. Now, my next goal is to populate the chart with json data. This is the code I'm using, which is inspired ...

Monitoring changes in session storage with AngularJS

In the sessionStorga, I have stored data from various controllers using a library called https://github.com/fredricrylander/angular-webstorage. The data is being successfully stored and is accessible within the current session. However, I am encountering a ...

A password must contain a minimum of 2 uppercase letters, 2 lowercase letters, 2 symbols, and 2 numbers in any order according to the regular expression

I'm having trouble implementing this RegEx pattern in JavaScript for password validation: (?=(.*\\d){2,})(?=(.*[A-Z]){2,})(?=(.*[a-z]){2,})(?=(.*[!@#$%^&*?]){2,})(?!.*[\\s])^.* ...

Is it possible to verify the value of the onclick attribute using jQuery?

Currently, I am facing a challenge with my ajax / php pagination system. I am struggling to apply the active class to the correct link in order to style it as selected. My question is, can I retrieve the value of onclick="" using jquery? Here is an examp ...

Get only the text content from a hyperlink using Tinymce-4

Within tinymce.activeEditor, I currently have this line of innerHTML code (part of a ul-list): <li><a href="#">Important words</a></li> When I place the caret within the sentence "Important words," and click a button with the foll ...

Utilizing Three.js to import models directly from a database

Currently, I have a MySQL database containing FBX models. The client-side of my application is able to read these models from the database. However, the model data is stored in a JavaScript variable. How can I utilize THREE.FBXLoader() to load the model da ...

What are the implications of an unidentified callback function with parameters?

Check out this snippet: const fs = require('fs'); fs.readFile('foo.txt', 'utf8', (error, data) => { if (error) { throw new Error(error); } console.log(data); }); Can you figure out where the anonymous callback is recei ...

Shade a selection of rows within the GridPanel Ext.net (ExtJS)

How can we highlight a specific set of rows in an Ext.net gridPanel by changing their color? Currently, the GridPanel renders using the following function: function (value, meta, record, rowIndex,columnIndex,store) { color= record.data["Col ...

Loading JSON data from a file in an AngularJS service

Looking to retrieve JSON data from a file named driverStandings.json, which can be found in the structure from the provided image. I am utilizing a service API to fetch this information by using the code displayed below (refer to image). https://i.sstatic ...

Tips on concealing Bootstrap 5 modal through button press

I've created a Bootstrap 5 modal that allows users to download a file by clicking a button. Everything works as expected, but I'm facing an issue with hiding the modal after the download button is clicked. I've been trying to use jQuery to ...

What is the reason for the promise being labeled as "undefined"?

I am currently working on developing a basic loader that, when triggered, interacts with an API to retrieve the necessary data. requestData(pageSize, page, sorted, filtered){ console.log(filtered); var url = `http://www.myurl.com/getProducts?start ...

Get the HTML code for the ReadMe file

Hey there, so I'm diving into the world of APIs for the first time and I stumbled across some interesting information on the GitHub API: It seems that READMEs can be accessed in custom media types to retrieve raw content or rendered HTML. Check it ...

Can an input element be used to showcase a chosen image on the screen?

I would like to display the selected image from an input element. Can this be done with a local file, accessing the image on the client side, or do I need to upload it to a server? Here is my React code attempt: I can retrieve the correct file name from t ...

Resetting input field based on callback function

In my simple script, I have two pages - script.php and function.php. The script.php page contains an input field with the ID #code, a link with the ID #submit, and a jQuery function. $('#submit').click(function () { $.ajax({ url: 'f ...

What is the best way to use Javascript in order to automatically open a designated tab within SharePoint 2013?

Currently, I am in the process of developing a website project which incorporates Bootstrap tabs utilizing jQuery. While these tabs are functioning excellently on various pages, I am faced with the challenge of linking specific icons to corresponding tabs ...

The links have IDs for scrolling to certain sections, but they are jumping over and missing some of the content

This particular project involves a website that consolidates all its pages/sections into one page. Each section has been assigned a unique ID (such as section1, section2, section3, etc.) and these IDs have also been linked to the top navigation href's ...

What is the solution for fixing the "Invalid Element Type" error?

Hey there! I'm currently working on creating a Twitter clone using React, but I've run into an error that's giving me some trouble. The error message reads: "Element type is invalid: expected a string (for built-in components) or a class/fu ...

Hovering over the child element, instead of the parent

I'm working on implementing a highlight feature for my website. The structure of the HTML looks something like this: <div> <!-- this is the main container --> <div> content ... </div><!-- a child element --> < ...

What is causing my Javascript media query for the sidebar to not function properly?

I am working on implementing a sidebar that slides out 250px using JavaScript on the desktop view. However, I want the sidebar to take up 100% width when viewed on mobile devices. Despite my attempts to use Media Queries in JavaScript to achieve this, I am ...