Regular expression pattern for 16-bit signed integer

I've been attempting to create a regex pattern for a 16-bit signed integer (from -32768 to 32767) based on the information from this 32-bit integer reference.

So far, my attempt looks like this:

^-?([0-9]{1,5}|32[0-6]{3}|32[0-6]{2}|327[0-5][0-8])$|^(-32768)$

Unfortunately, it's not working properly.

Any suggestions or guidance would be greatly appreciated.

Answer №1

It is recommended to try using an `if` statement instead of regular expressions for better performance.

let numberPattern = /^(-?(\d{1,4}|[012]\d{4}|3[01]\d{3}|32[0123456]\d{2}|327[012345]\d{1}|3276[01234567])|-32768)$/;

// test
console.log("-32768", numberPattern.test("-32768")) ;
console.log(" -9876", numberPattern.test("-9876")) ;
console.log("  9876", numberPattern.test("9876")) ;
console.log(" 32767", numberPattern.test("32767")) ;
console.log("-32769", numberPattern.test("-32769")) ;
console.log(" 32768", numberPattern.test("32768")) ;

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

React: Dealing with null values when making a PUT request using axios

I have a list obtained from an external API through the use of axios. Each element in the list is an editable input field with its own corresponding update button. Upon modifying the data in an input, and while executing a PUT request to update it, consol ...

Steps for showcasing the data entered in the input box

I am looking to enhance my skills in working with Angular and JavaScript. I would greatly appreciate any feedback on the issue I am facing here. My goal is for the text box input to display: "hello {name} , would you like to play a game? However, curr ...

Unravel the encoded string to enable JSON parsing

Here is an example of my JSON string structure [{"id":0,"nextCallMills":0,"delay":0,"start":"... I am facing an issue with JSON.parseString() Even after trying unescape() a ...

Understanding the Use of Promises and Async/Await in Typescript

Struggling with asynchronous libraries in Typescript, I find myself looking for a way to wait for promises to be resolved without turning every method into an async function. Rather than transforming my entire object model into a chain of Promises and asyn ...

Creating a regular expression that can successfully identify numbers with commas after every three digits

As a newcomer to Python and regular expressions, I've taken on an exercise that presents the following challenge: How can a regex be written to match numbers with commas every three digits? It should successfully match the examples below: '42&a ...

Here is how you can include a date picker with the ability to choose the day, month, and year

Can someone help me create a datepicker with options to select days, months, and years? I've been able to find resources for selecting months and years, but I'm having trouble adding the option to choose specific days. If anyone has experience ...

Is Selenium failing to scrape the final webpage within the loop due to Regex in Python?

I am currently working on a basic Selenium scraper. The main goal of this project is to verify the presence of a "contact" link, and then, if found, extract any emails using Regex. If no contact link is present, the program should parse the initial page lo ...

The Jquery tooltip feature consistently displays the title of the header as the tooltip

Currently, I am utilizing the default Jquery tooltip library, and it functions wonderfully with one exception. In Firefox, the tooltip always displays the title of the page (within <head>). For example: <title>My Title</title></head> ...

Unable to insert a JSON object into an array using JavaScript and MongoDB

I'm encountering an issue when trying to push data into my Student model with the following schema: var StudentSchema = new Schema({ firstName: { type: String, trim: true, default: '' //validate: [validat ...

The AngularJS 1.5 component modal with a callback function is experiencing issues with updating Angular bindings when called multiple times by an embedded object in IE11

Encountering an issue in IE 11 with an Angularjs 1.5 modal component where the binding is not updating as expected when multiple callbacks are made from an embedded object call. The console messages show that the callbacks are being executed, but the templ ...

Implementing CSS styles on iframe content in React

Is there a way to style the content loaded by an iframe in a React app using CSS? I'm facing an issue with loading external content into my app, as the styles are outdated and I want to customize them. See Example Below: (Please note that the src co ...

Switching the parameter to navigate to the appropriate page

Hi there, I'm looking to implement a quick change using the onchange parameter. Can someone assist me in using the onchange parameter to navigate to a new page? So, if someone selects for instance Basic info, they will be directed to a new page custo ...

Unable to successfully scroll a webpage using Selenium Webdriver or Javascript Executor

`from selenium import webdriver from selenium.webdriver.common.keys import Keys from selenium.webdriver.common.by import By from selenium.webdriver.common.action_chains import ActionChains import time driver = webdriver.Chrome('chromedriver.exe' ...

Tips on triggering a function defined within an AngularJS scope using an event handler

I am in the process of creating a custom directive that generates a menu by processing an array of menu entries defined as objects. The menu works perfectly fine and appears as a pop-over above the page content. Everything seems to be in order so far. Ho ...

Press the Javascript URL submission button

Currently utilizing the Free Bootstrap Wizard tool, found at The wizard code can be accessed at In my current project, I am attempting to redirect users to the register-success.html page once they click on the "finish" button. Below is a snippet of the ...

Using React, update the checkbox so that it dynamically changes from checked to unchecked

Hey there, I'm currently working on a table that includes checkboxes. The idea is that when I click on "Add Entries", a modal opens up; and when I click on "Done", I want the checkboxes to become unchecked without having to refresh the page. Do you ha ...

Exploring the Difference Between __proto__ and __proto__ Accessor in MDN Documentation

According to the information provided by mdn in their documentation on JavaScript's Inheritance and the prototype chain, it is stated All objects inherit the Object.prototype.__proto__ setter, which can be used to set the [[Prototype]] of an existin ...

To link the information within the angularJS controller

I've recently generated a div element dynamically within the controller function of my AngularJS application. However, I'm facing an issue where the data is not binding as expected inside this div element. Here is a snippet of my code: function ...

Error: Unexpected character 'o' encountered in AngularJS syntax

Whenever I try to call this controller, it gives me an error. hiren.controller('hirenz' , function($scope , $http , $location , $routeParams){ $http.post((rootURL + "music") , {'alpha' : $routeParams.alpha , 'name' : $rou ...

The webpack vue-loader throws an error message stating "unexpected token {" when processing a single-page .vue component

As a C# backend developer, I am venturing into the world of Vue.js. I typically work with Visual Studio 2017 + ASP.NET MVC (using it as an API + one layout) + Vue.js + Webpack. .vue single-page component files are loaded by vue-loader, while .js files a ...