Utilizing Typescript for extracting coordinates from regular expressions

I'm currently working with Typescript and I've been exploring the idea of using regular expressions for a certain task.

The input data is in the form of a string:

"(0,1)(1,2)(3,1)"

There are several validations that need to be performed on this data:

  1. Every value should be a numeric digit.
  2. Each number must not exceed a certain variable value x set at runtime.
  3. If all validations pass, I would like to extract the matches into an array while removing the parentheses around them.

Do you think this can be achieved using regular expressions?

Answer №1

Having a JavaScript question, you can make use of regular expressions and additional logic to achieve the desired solution:

const validateCoordinates = (inputString: string, maxValue: number) => {
    const match = inputString.match(/\((\d+),(\d+)\)\((\d+),(\d+)\)\((\d+),(\d+)\)/);
    if (match) {
        const results = match.slice(1, 7);
        const pass = results.every(item => Number(item) < maxValue);
        if (pass) {
            return results.map(n => Number(n));
        }
    }

    return false;
};

validateCoordinates('(1,1)(1,2)(3,1)', 3); // false 
validateCoordinates('(1,1)(1,2)(3,1)', 5); // [ 1, 1, 1, 2, 3, 1 ]
validateCoordinates('(1,1)(1,2)(3,1s)', 5); // false

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

Encountered an issue when attempting to include the "for" attribute to a label within an angular 2.0 template

I am currently using Angular 2.0 framework and working on developing an input component. In addition to that, I am also utilizing Google MDL which requires a specific HTML structure with labels for inputs. However, when trying to implement this, I encounte ...

The styles from bootstrap.css are not displaying in the browser

Currently in the process of setting up my angular 2 project alongside gulp by following this helpful tutorial: I've added bootstrap to the package.json, but unfortunately, it's not reflecting in the browser. I can see it in the node_modules and ...

Angular Tutorial: Understanding the Difference Between TypeScript's Colon and Equal To

I've been diving into the Angular4 tutorial examples to learn more about it. https://angular.io/docs/ts/latest/tutorial/toh-pt1.html One of the code snippets from the tutorial is as follows: import { Component } from '@angular/core'; @Co ...

Error encountered during the deployment of Ionic 3 with TypeScript

After completing the development of my app, I ran it on ionic serve without any issues. However, when compiling the app, I encountered the following error message. Any assistance in resolving this matter would be greatly appreciated. [15:40:08] typescri ...

I encountered a TypeScript error when trying to make an HTTP post request using Angular

In my Angular 5 application, I am trying to interact with an API using a service. I have a method that is intended for posting data to the API. However, when I specify the type of the post method as "Candidate" object, I receive the following warning: src ...

`TypeRoots` in Typescript are neglected when `noImplicitAny` setting is enabled

I always set up my tsconfig.json with the strict parameter set to true. This means that noImplicitAny is also automatically set to true. However, I have noticed that when strict is enabled, TypeScript does not seem to recognize the typeRoots entry for loca ...

Mastering Ember with Typescript - crafting action definitions

Trying to set up my first Ember app using TypeScript, I'm facing issues with defining actions. Most tutorials suggest using the @action decorator like this: import { action } from '@ember-decorators/object'; @action sayHello(){ } However, ...

What is the best method for accessing a specific key value in a JSON file?

I am looking to extract a specific field data from a JSON list Here is the example JSON data: { "data": [ {"answer": "Yes","email": "aaaa.cpm","color": "Maroon"}, {"answer": "No","email": "bbbb.cpm","color": "White"}, {"answer": "Yes","email" ...

Issue regarding locating Material-UI components while resolving TypeScript modules

I am encountering an issue with my Visual Studio 2019 (v16.3.2) React TypeScript project that involves Material-UI components. The TypeScript compiler is unable to resolve any of the @material-ui imports, resulting in errors like the one below which preven ...

Events are not being bound by the JQuery on() method, despite successful execution in the console

I'm currently working on generating a button dynamically using TypeScript code. The button appears on the page, but unfortunately, none of the events associated with it seem to work properly. I have looked into delegation and implemented it as advised ...

I'm looking for a solution to reorganize my current state in order to display the image URL

My React component, which also utilizes TypeScript, is responsible for returning a photo to its parent component: import React, { useEffect, useState } from "react"; import axios from "axios"; export const Photo = () => { const [i ...

Verifying the accuracy of a React Component in interpreting and displaying a path parameter

I have the following React/Typescript component that is functioning correctly. However, I am struggling to write a test using testing-library. My goal is to verify that it properly receives the level parameter and displays it on the page: import React from ...

How do I repeatedly display an HTML element using a loop in Typescript?

As a newcomer to Typescript, I am attempting to create a function that generates multiple buttons based on the data stored in an array. Initially, I tried using a for loop like this: splitLabels(Array: any){ if (typeof Array != "undefined& ...

Navigating the intricacies of debugging sub-domains in Angular projects using Visual Studio Code (VS

Currently working on a massive project utilizing micro-services. The unique design for clients/tenants requires visiting their specific subdomain to select a particular tenant. For example, https://ClientA.localhost:4200 and https://ClientB.localhost:4200. ...

Utilize the express library (NodeJS, TypeScript) to send back the results of my function

I am curious about how I can handle the return values of my functions in my replies. In this specific scenario, I am interested in returning any validator errors in my response if they exist. Take a look at my SqlCompanyRepository.ts: async create(compan ...

Display a picture retrieved from a POST request using Angular and a Spring Boot backend

Recently, I've been delving into the world of HTTP requests. My latest task involves uploading an image from the client and sending it to the server using a POST request. When testing this process in Postman, everything works smoothly as I receive th ...

Modify the value of the <select> tag based on whether it is required or not

When utilizing a my-select.component within a form component, the following code snippet is used: <div *ngIf="items?.length"> <select [ngModel]="selectedItem" (ngModelChange)="valueChanged($event)"> <optio ...

The customized chart component is experiencing difficulties rendering the chart

Recently, I started working with React TypeScript and I'm currently focused on rendering charts using custom components. One of the components I am working with is called StackedDonutChart which utilizes Highcharts. `export interface StackedDonutChart ...

The error message "Property 'showUserDropdown' is not found on type '{}'.ts" indicates that the specified property is not present in the defined

While creating a basic Vue component, I encountered an error in the IDE regarding the {{ showUserDropdown }} with the message: Property 'showUserDropdown' does not exist on type '{}'.ts Despite adding it to data, <template> &l ...

Adjusting the ng-turnstile Dimensions

Looking for a way to adjust the width of CloudFlare Turnstile to match its parent container's layout without causing any issues with the Iframe. Is there a more efficient method to achieve this? The current solution I have seems to be messy: import { ...