What is the alternative functionality of the "g" flag in the test method of RegExp?

http://jsfiddle.net/bpt33/

var t = "";

var a = ["atom-required","atom-label","atom-data-type","atom-regex"];

var r = /atom\-(label|required|regex|data\-type|class|is\-valid|field\-value|error)/i;

function test(a, r){
    for(var i = 0; i<a.length; i++){
        t += a[i] + " => " + r.test(a[i]) + "<br/>";
    }
}

test(a, r);

t += "<br/>";

a = ["atom-required","atom-label","atom-data-type","atom-regex"];

var r = /atom\-(label|required|regex|data\-type|class|is\-valid|field\-value|error)/gi;

test(a, r);

$("#results").get(0).innerHTML = t;

When not using the global flag (g), the script runs correctly,

atom-required => true
atom-label => true
atom-data-type => true
atom-regex => true

However, when the global flag is specified, it behaves differently,

atom-required => true
atom-label => false
atom-data-type => true
atom-regex => false

Answer №1

When the g modifier is used, the regular expression becomes stateful and continues searching from the index after the last match.

If no match is found, it resets itself.

To see where the search will start next, you can check the value of the .lastIndex property.

r.lastIndex; // 0 or another number

You can manually reset it by setting the property to 0.

r.lastIndex = 0

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

Using Ant Design Table component: How to pass a string to the render method in the column attribute

I am currently utilizing Ant Design in my React project and I wish to incorporate a string into the render attribute for the column structure. Here is a sample code snippet. import React from 'react'; import ReactDOM from 'react-dom'; ...

Global installation of npm packages: Step-by-step guide

I've been interested in trying out Vue.js, but I'm struggling to figure out how to integrate it into my project. Is there a way to install the dependencies without requiring an internet connection to download them again, especially when they&apos ...

Tips for populating class attributes from an Angular model

Suppose there is a Class Vehicle with the following properties: public id: number; public modelId: number; public modelName: string; Now consider we have an object that looks like this {id: 1, modelId: 1, modelName: "4"} What is the best way to assign e ...

Regular Expressions: Capturing a section enclosed by an equal number of opening and closing braces

I am facing a challenge on a page that contains a lot of text. My goal is to identify any text enclosed between two sets of double braces ({{}}) and then move the entire block to the very top of the page. This task becomes more complicated because the bloc ...

Focusing solely on a particular category in EJS

I am struggling with this code snippet. HTML: <header<% if ( current.source === 'features' || current.path[0] === 'index' || current.source !== 'customers' ) { %> class="header-white"<% } %>> <div cl ...

Tips for displaying multiple videos on an HTML webpage

I am trying to set up a video player for videos in 720p resolution (1280x720) with autoplay and looping so that once one video ends, the next one from an array will start playing. However, I am encountering issues where the first video does not autoplay an ...

Troubleshooting issues with integrating CDN in a NextJs 13 project

Struggling to add a CDN like Remix Icon to my NextJs13 project. Initially tried adding it by importing the 'head' tag and inserting values into the Head component, but that method is no longer working. There seems to be a new way of doing it now, ...

Error: The operation 'join' cannot be performed on an undefined value within Fast2sms

I am encountering issues while attempting to send SMS using fast2sms in Node.js. The error message reads as follows: TypeError: Cannot read property 'join' of undefined at Object.sendMessage (C:\Users\user\Desktop\node_module ...

The challenge of overlapping elements in jQuery UI resizable

Encountering an issue with the resizable class in jQuery UI (http://jqueryui.com/resizable/). When resizing begins, all divs below move to overlap with the resizing div. Upon inspecting the js console in chrome, it appears that resizable applies inline sty ...

"Troubleshooting: Why is my Bootstrap modal window only printing a

I've encountered an issue with printing the content of a Bootstrap modal window. Previously, the code I had was working fine but now it seems to be malfunctioning. Content is dynamically added to the form using appendChild() in a separate function. Ho ...

Iterating over objects within a nested array using ng-repeat

My back-end is sending me an array of string arrays (Java - CXF-RS). The length of each array within the string arrays ranges from 1 to n. In order to display this data on my front-end, I am utilizing ng-repeat. Everything is functioning correctly with o ...

Angular directive incorporating a reset feature

Need help with a directive I created called filterButton. It allows users to select filter options and adds a check mark on the right. However, I am struggling with resetting all filter fields back to default when the reset button is clicked on filter.html ...

Exploring the power of VueJs through chaining actions and promises

Within my component, I have two actions set to trigger upon mounting. These actions individually fetch data from the backend and require calling mutations. The issue arises when the second mutation is dependent on the result of the first call. It's cr ...

Utilizing a c++ xpcom component in conjunction with a javascript xpcom component

After developing an XPCOM component using C++ with a GetHWND() method, I am now working on another XPCOM component using JavaScript. My goal is to utilize the GetHWND function from my C++ component in the JavaScript XPCOM component. Here is the code snippe ...

Node js guide: Generating secure passwords with nonce, timestamp, and password

Currently, I am working on developing an app using Express and need to make a SOAP API request. The API requires sending nonce, timestamp, and digest password. Initially, I successfully implemented this functionality using PHP. Now, I want to achieve the s ...

Drop draggable items on top of each other

I'm wondering if there's a way to drag jQuery elements into each other. To illustrate my question, I've duplicated this code (link) and made some style changes. Here is the updated version: Fiddle Link. In the current setup, you can drag e ...

Sending information to a singular Vue component file

Struggling to pass data to props and render different content in a component on each page? Want to display links, tech, feedback without deleting the component or rewriting code for individual pages? Existing methods not working for you? Unsure if what you ...

Transform an SVG string into an image source

Is there a way to convert an incoming string ' ' into an img src attribute and then pass it as a prop to a child component? I attempted the following method, but it hasn't been successful. `let blob = new Blob([data.payload], {type: &apos ...

Learn how to effortlessly move a file into a drag-and-drop area on a web page with Playwright

I am currently working with a drag-zone input element to upload files, and I am seeking a way to replicate this action using Playwright and TypeScript. I have the requirement to upload a variety of file types including txt, json, png. https://i.stack.img ...

Having trouble resolving errors in Visual Studio Code after failing to properly close a parent function? Learn how to fix this issue

Here we have the code starting with the construct function followed by the parents function public construct() //child { super.construct; } protected construct() //parent { } The issue arises where I am not receiving an er ...