What is the pattern for identifying numbers that are followed by an underscore and then more numbers?

I need help creating a regular expression for JavaScript validation. The pattern I'm looking to match is numbers followed by an underscore, and then more numbers.

For example: 123456789_123456789

The length of the string can vary - it can have any number of digits, an underscore, and then another set of digits. I've tried using [0-9]_[0-9], but I'm wondering if there's a better way to achieve this. Any suggestions would be greatly appreciated.

Thank you, Sreekanth

Answer №1

You're close! The correct regular expression should be:

^[0-9]{1,}_[0-9]{1,}$

or

^[0-9]+_[0-9]+$

This regex pattern indicates: "at least one digit ([0-9]{1,}), followed by an underscore (_) and then at least one more digit ([0-9]{1,}).

It will match:

12312_123123
1_1

but won't match:

123123_
_123123
_
123123_1231ddd
123dd_123
dd123_123

Answer №2

In case the numbers are not required: /^\d*_\d*$/, otherwise: /^\d+_\d+$/.

Here are some examples:

/^\d+_\d+$/.test("123_");    // returns false
/^\d+_\d+$/.test("123_123"); // returns true

Answer №3

Here is the attempt you made: [0-9]_[0-9]

Which means,

The correct answer should be: [0-9]+_[0-9]+

So basically,

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

Nodejs: code functions properly at my residence, but encounters ECONNREFUSED error when executed in the workplace

Completely new to JavaScript and Node.js, I decided to follow a guide on web scraping with Node.js from . The code worked perfectly at home, but when I tried to implement it at work, I encountered a persistent issue. Error: connect ECONNREFUSED at errnoEx ...

When I type in the TextBox, the CheckBox value should be deactivated if it is unchecked

Whenever I attempt to input text into the textbox associated with my checkbox, the checkbox automatically becomes unchecked. How can I prevent this from happening? <span id="TRAVEL_TYPE_1"><input id="TRAVEL_TYPE_0" type="checkbox" onclick="Update ...

Query parameter is not defined

Can anyone assist me with extracting the ean from the following URL: This NodeJS server processes the request in the following manner: const http = require('http') const port = 3000 const requestHandler = async (request, response) => { ...

Executing an SQL query within a JavaScript function, what's the best way to do

Similar Question: How do I connect to a SQL server database using JavaScript? I am wondering how I can execute an SQL query within a JavaScript function. Any assistance would be greatly appreciated. Thank you. ...

Issues with the functionality of jQuery's .load() method are causing

I am encountering an issue for the first time. Inside ajax.html, I have the following code in the header: $(document).ready(function(){ $( "#result" ).load( "/loaded.html" ); }); In the same directory, there is a second page named loaded.html: <d ...

What is the counterpart of .contains for string buffers?

Is there an alternative method to .contains for a StringBuffer instead of a String? For instance, when working with a String, you can use: String word = "word"; if(word.contains("w") { //Perform some action } However, if you convert 'word' in ...

What level of detail is optimal for my model?

What is the best approach for structuring data models in Meteor? For example, let's consider a data model with a XmlDocument containing multiple XmlNodes. Should I create a single collection like new Meteor.Collection("Documents") and update it as a ...

How to remove a specific item from an array in MongoDB using JavaScript

So, I've retrieved a nested array from a database query. The query brings back the following data: { _id: new ObjectId("623rf3f22275f399f88bb"), first_name: 'Are', last_name: 'You', email: '<a href="/cdn-cgi/l/email ...

Utilizing jQuery event delegation with the use of .on and the unique reference of (

Questioning the reference in a delegated .on event: Example: $('#foo').on('click', $('.bar'), function() { console.log(this); }); In this scenario, `this` will point to #foo. How can I access the specific bar element tha ...

managing information through the elimination of nested keys with node js / javascript

let json=[ { metadata: { tags: [] }, sys: { space: { sys: [Object] }, id: '4gSSbjCFEorYXqrgDIP2FA', type: 'Entry', }, fields: { richTextEditor: { 'fn-US': [Object] }, short: { 'as-ds': &a ...

Can a managed MUI TextField be programmed to output an object as its value?

I am currently working on creating a custom input component using the MUI TextField. This input component allows for switching between languages so that values can be edited for multiple languages. However, I am encountering an issue where I am only able t ...

Generate an array containing all N-digit numbers with a sum of digits equal to S

I encountered a problem with my code translation process. Despite finding solutions in other languages, when I converted them to javascript, the expected array was not created. const find_digits = (n, sum, out, index) => { if (index > n || sum & ...

What could be causing the updated JavaScript file to not run on a live Azure website using ASP.NET MVC?

After making a minor adjustment to my JavaScript file on my deployed ASP MVC website hosted on Azure, I decided to redeploy everything. However, upon checking the resource files, I noticed that the JavaScript had indeed been changed, but the behavior of th ...

Can you provide guidance on how to showcase a list?

What is the correct way to display a list received through a URL in JSON format? Here is an example project. When using a local variable everything works fine, but when trying to fetch the list from a URL, an error is displayed. constructor(props) { ...

Introduction to redirection in Node.js: A beginner's guide

When a user clicks on a link on my website, they are directed to the registration page with the following code... //register.jade a(href="/register") Register //app.js (server) var reg = require('./routes/register'); ... app.use('/registe ...

Tips for including a new class in an HTML element

My goal is to add a specific class to an HTML tag, if that tag exists. This is the code I have attempted: <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>index</title> <style> ...

How does a JSONP request differ from an AJAX request?

I have searched extensively for a solution to the matter mentioned, yet I did not come across anything intriguing. Would you be able to clarify it in simple terms? ...

Error: Attempting to modify a constant variable

Can anyone assist me in resolving this error? I have included my index.js, routes.js, and db.js code along with the error description. I've been putting in a lot of effort to troubleshoot this error. index.js const express = require('express&a ...

How to send arrays through JSON?

I have some PHP code: $ids = array(1,2,3); $names = array("cat","elephant","cow"); $originalSettings = array ('ids'=>$ids,'names'=>$names); $jsonSettings = json_encode($originalSettings); echo $jsonSettings; Here ...

Not defined within a function containing arrays from a separate file

Can anyone help me with listing multiple arrays from another file? When I try to iterate through each array using a "for" loop, the code compiles and lists everything but gives an undefined error at the end. How can I fix this? I have included some images ...