Specific Character Count in Regular Expressions

THIS IS DISTINCT FROM THE REFERENCED QUESTION because I have extensively researched and tested various solutions before resorting to posting this inquiry.

I am in need of a validation for inputs that adhere to the following criteria:

The first character is always: 0
The second character is always: 1
Followed by exactly 9 digits

For example:

case 1: 01123456789 => should match

case 2: `01123456789`0 => should not match but does match

case 3: 123`01123456789`5985 => should not match but does match

I am currently using the following pattern:

/([0][1][0-9]{9})/g

However, both case 2 and 3 are also matching with this pattern, when only case 1 should be accepted. How can I adjust the pattern to achieve this?

I am utilizing this tool to test the patterns

Answer №1

Make sure to employ the correct anchoring technique (\b is recommended in this scenario for 'word boundary'):

/\b01\d{9}\b/g

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

Escaping an equal sign in JavaScript when using PHP

I am currently working on the following code snippet: print "<TR><TD>".$data->pass_name."</TD><TD><span id='credit'>".$data->credit_left."</span></TD><TD><input type='button' val ...

When exporting an enum in StencilJS with TypeScript, the error "Cannot find name..." may occur

Looking for a solution: https://github.com/napolev/stencil-cannot-find-name In this project, there are two main files to consider: custom-container.tsx import { Component, Element, State } from '@stencil/core'; @Component({ tag: 'cu ...

Proper Structure for Node System (BASIC)

Overview Recently, I entered the world of Node.js and built some basic back end functionality. However, I realized that everything was clustered in one file (index.js) which led me to explore tutorials on using express router middleware and adapting a mod ...

Starting a tab just once

I have successfully created 4 static HTML pages that include: Home About Services Contact Each page contains a link that leads to another static page named myVideo.html, which plays a video about me in a new tab. The link is available on every page so ...

The data type 'unknown' cannot be assigned to the type 'any[]', 'Iterable<any>', or (Iterable<any> & any[])

I have been working on creating a custom search filter in my Angular project, and it was functioning properly. However, I encountered an error in my Visual Studio Code. In my previous project, everything was working fine until I updated my CLI, which resul ...

When using CasperJS to capture multiple screenshots, the most recent screenshot will replace all previous ones

Exploring CasperJS has been a great experience for me. Despite my enjoyment, I've encountered an issue with casper.capture() that has me stumped. I've set it up to capture screenshots whenever a test fails and placed it in a separate setup module ...

Can the combobox be used for organizing items?

Can someone help me simplify this code? When 'info' is selected from the combobox, the corresponding articles should be displayed, and the same goes for 'Berita'. Here's the lengthy script I have, using the post method. Any suggest ...

Guide on diverting response from an ajax request

I have a situation where I need to redirect to a page based on a response. I have successfully made an ajax call and can handle the success part. The response contains an html page, but I'm unsure of how to redirect to that page. Below is the code I ...

Exclude objects with similar ids using a method other than JavaScript's filter()

I have a list of students who need to receive medication multiple times throughout the day. The challenge is to filter the array to track how many times each student receives the same medication. The list includes three different students and repeats the s ...

The HTMLInputElemet type does not include a Property Rows

Hey there, I'm just starting to dive into Angular and TypeScript. My current challenge is figuring out how to check if a table is empty or not so that I can show or hide a specific div accordingly. I've attempted the following: var rows = docume ...

Empty data passed to Highcharts xAxis labels formatter callback function

I am new to using Highcharts and I am trying to utilize the xAxis.labels.formatter function to generate labels. However, I am facing an issue where the formatter function is running with empty data. Interestingly, when I click on the legend, the data gets ...

Obtain a sanitized response object from a POST request using Restangular

Having an issue with the Restangular service. When making a POST request, the response Object contains all the Restangular methods which I don't want. I just need a clean response without any extra methods. Any suggestions on how to achieve this? th ...

Exploring the usage of slots in WebComponents without relying on shadow DOM

I am working on developing a WebComponent without utilizing ShadowDOM. So far, everything has been going smoothly. However, I now aim to create a component that wraps other components similar to how it is done in Angular with @ViewChild / @ViewChildren. (I ...

Why is my event.target.value not updating correctly in React useState?

My problem is that when I use useState, I am receiving incorrect numbers For example, if I print e.target.value it might display 1, but my selectedIndex shows 2. Similarly, when I have a selectedIndex of 0, it retrieves something different like 1. Any tho ...

Exploring Angular's Observables for handling HTTP requests

Exploring Angular Observable Documentation As I delved into the Angular observable documentation mentioned above, I came across a section that compares promises with observables. Observables provide many values, while promises provide one. This characte ...

Javascript: Comparing Equality of two Objects/ Arrays

Consider two objects with the same property: var obj1 = {name: 'John'}, obj2 = {name: 'John'}; Result: obj1 == obj2; and obj1 === obj2; both return false The same holds true for arrays: var arr1 = [1, 2, 3], arr2 = [1, ...

Tips for saving the ajax response to the browser cache using jquery

I am currently working on storing the ajax response in the browser cache. I have successfully managed to store it for URLs like (example.com), but now I am facing a challenge with storing data for URLs like (example.com?xyz=abc%20efg&mno=hjk). Here is ...

Using PHP to dynamically update image source upon clicking an upload button:

Is it possible to dynamically display a new image after uploading it using an ajax-upload script on a 'edit product' page for a shop products? On load, all thumbnails are fetched from the DB but when a user uploads a new file, I want the new imag ...

Conceal a div while revealing the other div

I am currently trying to achieve a unique visual effect. I have three divs named div1, div2, and div3. The objective is for div1 to slide up and disappear when clicked, while div2 simultaneously slides up and becomes visible. Similarly, when div2 is click ...

Struggling with calling rerenderEvents in FullCalendar using JQuery after a successful AJAX request

I seem to be encountering an issue where the calendar does not update after a POST request. Everything works smoothly until that point: $('#calendar').fullCalendar({ ... select: function (startDate, endDate) { $.ajax({ ...