Remove the initial x characters from a numerical value and verify if it aligns with a specified regular

I need to remove the initial 6 characters (numbers) from a number and verify if it matches any number on a given list. For instance, if we have a number input like: 1234567891234567

The first 6 characters extracted would be: 123456

Then I want to confirm if 123456 is equal to any of these numbers: 123456|765321|988721.

To extract the first n digits, I can use this pattern: \d{6}. After that, I'll need to verify if those 6 digits match one of the numbers in the list.

Answer №1

To ensure that the regular expression matches only at the start of the string, you can include a start of string anchor ^.

const regexp = /^(123456|765321|988721)/; 

console.log(regexp.test('1234567891234567'));
console.log(regexp.test('7653217891234567'));
console.log(regexp.test('qq'));

Answer №2

When you have already extracted the 6 digits from the beginning of a string using ^\d{6}, you can then utilize the split method and iterate through the values like this:

"123456|765321|988721".split('|').forEach((s) => {
  console.log(s === "123456");
});

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

What are the security benefits of using Res.cookie compared to document.cookie?

When it comes to setting cookies to save data of signed in members, I faced a dilemma between two options. On one hand, there's res.cookie which utilizes the Express framework to set/read cookies on the server-side. On the other hand, there's d ...

Search for JSON keys in the output data stream

Consider this example JSON file: { "mac": "00:11:22:33:44:55", "name: "Test123", "ssid": "29321", "password": "txt", "data": { "test:": "no", "dev": "yes", "prod": false }, "signals": [12, 34, 65, 93, 21 ...

The datepicker is refusing to update the date format

I've been attempting to adjust the date format for my datepicker, but it refuses to change. Below is the code I'm using: $(document).ready(function() { $('#dateselect').datepicker({ format: 'dd/mm/yyyy', o ...

Scrolling automatically within a child element that has a maximum height limit

I am currently developing a console/terminal feature for my website. https://i.stack.imgur.com/AEFNF.jpg My objective is to allow users to input commands and receive output, which might consist of multiple lines of information. When new output is displa ...

"Troubleshooting VueJS: Issue with default value not being set in

Trying to set a default value for a select in Vue. <script src="https://unpkg.com/vue"></script> <div id="app"> <select v:model="select"> <option value="1">1</option> <option value="2">2</optio ...

When you try to import from another file, the method is not defined

When I attempt to import a method from another file, I am encountering an issue where it returns undefined. The structure involves 3 files with one calling the next in sequence. File1: const { methodFromFile2 } = require('./file2'); methodFromFi ...

What is the process for resolving arguments in UI-Router's resolve function?

There seems to be a gap in my understanding of UI-Router and angular's documentation, so forgive me if this seems naive, but here goes: On http://angular-ui.github.io/ui-router/site/#/api/ui.router.state.$stateProvider, there is an example resolve fu ...

Unable to create a pie chart with chartjs using JSON data

I am having trouble creating a pie chart using canvas.JS. I have already tried iterating through them in a loop. The JSON data returned looks like this: [[{"label":"belum","x":1},{"label":"sudah","x":5}]] Below is the code I am using to retrieve the JS ...

What is the method for triggering two actions within a single linked tag?

I have a link tag structured like this: <a href="<?php echo base_url().'dashboard' ?>" class="check_session">Home</a> Upon clicking the "Home" link, it should navigate to the dashboard. At the dashboard page, I want to check i ...

Having trouble with Ajax.updater?

I am a JavaScript newcomer and currently facing an issue with prototypes. I am attempting to update sample.jsp using Ajax.updater after it is loaded, but for some reason, it's not working. Here is the source code of sample.jsp: <%@page contentTyp ...

Ways to automatically display the date upon loading the view

I have integrated a plugin with Angular, which is essentially an extension. Upon loading the view, only an empty input is visible. Clicking on the input reveals a calendar with today's date displayed. My objective is to automatically load today&apos ...

Updating the background image without having to validate the cache

I have implemented a basic image slideshow on my website using a simple Javascript function. The function runs every 5 seconds to update the CSS "background-image" property of a div element. While it is functional, I've noticed that each time the func ...

Nightwatch is a tool that allows you to interact with elements on a webpage by clicking on an element within

I have a scenario on my website where I have two separate div elements: <div class="wg-block" data-reactid="10" <div class="wg-header" data-reactid="11"/div> .... <h4 class='condition'> "Text" </h4> <div cl ...

Encountering a problem with ng-repeat when working with a JSON

Having a bit of trouble displaying the keys and values from a JSON object in an HTML table using ng-repeat. The JSON object is coming from the backend, but for some reason, it's not showing up on the frontend. I know there must be a simple mistake som ...

Changing Axios requests to send data as a JSON objectDo you need to know how

Looking at the title, there is a scenario where you execute a axios.post('/user', { firstName: 'Fred', lastName: 'Flintstone' }) .then(function (response) { console.log(response); }) .catch(function (erro ...

Unable to access npm run build on localhost

I have developed a web application using react and node.js, and now I want to test it with a production build. After running npm run build in the app directory, I successfully created a build folder. However, when trying to run the application using local ...

Modifying the size of an element with D3 when hovering with the mouse

In a previous project, I created a stacked bar chart with some interesting mouseover effects. Now, I want to take it a step further by changing the size of the rectangle that the user hovers over, returning it to its original size once they move away. My ...

Disabling a button based on the result of a database query

Is there a way to dynamically enable or disable a button based on the result of a database query in JavaScript? I have managed to display an error message (with id="error") depending on the result, but toggling the button (with id="generate") doesn't ...

MUI Error: Incorrect prop provided to menu item

I am encountering an issue with a React component that generates a list of elements, each containing a button to open a menu. The problem is that the props being passed to each menu are incorrect; they always reflect the props of the last element in the ...

Pressing the button will activate the Ctrl+z and Ctrl+y key commands

I have created two separate buttons for triggering actions equivalent to pressing Ctrl+z and Ctrl+y. I am attempting to make these actions occur with the click of a single button. However, when trying to set up the functionality to trigger Ctrl+z and Ctr ...