Using Regex to detect recurrent (similar) patterns in jQuery <input> tags

I need assistance with ensuring that users input article numbers in the correct format, like this:

ABC_ABC123; AB__B2A4; ABF_323WET; ...

Currently, I have a regex pattern that works for one article number:

var mask1 = /^([A-Z]{2})([A-Z|_]{1})_([A-Z0-9]{0,16});$/;

However, I am looking to extend this pattern to accommodate multiple strings/art numbers. How can I achieve this?

Your help is greatly appreciated! Thank you!

Answer №1

There seems to be an issue with the lowercase letters not matching in your single item pattern. To address this, ensure that they are included in the character class, for example changing [A-Z0-9]{0,16} to [A-Za-z0-9]{0,16}.

The revised overall solution will appear as follows

/^[A-Z]{2}[A-Z_]_[A-Za-z0-9]{0,16};(?: [A-Z]{2}[A-Z_]_[A-Za-z0-9]{0,16};)*$/

For a demonstration and testing of the regex pattern, you can check the regex demo here

Additional Details:

  • ^ - indicates the beginning of the string
  • [A-Z]{2}[A-Z_]_[A-Za-z0-9]{0,16};
    - specifies the item pattern:
    • [A-Z]{2} - two uppercase ASCII letters
    • [A-Z_] - either an uppercase ASCII letter or _
    • _ - represents a _ character
    • [A-Za-z0-9]{0,16} - allows for 0 to 16 alphanumeric ASCII characters
    • ; - indicates a semi-colon
  • (?: [A-Z]{2}[A-Z_]_[A-Za-z0-9]{0,16};)*
    - denotes zero or more occurrences of
    • - signifies a space (can be replaced with \s to match any whitespace)
    • [A-Z]{2}[A-Z_]_[A-Za-z0-9]{0,16};
      - represents the item pattern mentioned above
  • $ - marks the end of the string.

In JavaScript, it is advisable to construct the pattern dynamically for better readability:

var block = "[A-Z]{2}[A-Z_]_[A-Za-z0-9]{0,16};"
var regex = new RegExp(`^${block}(?: ${block})*$`)
console.log( regex.test("ABC_abc123; AB__B2A4; ABF_323WET;") )

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

Issue with populating labels in c3.js chart when loading dynamic JSON data

Received data from the database can vary in quantity, ranging from 3 to 5 items. Initially, a multi-dimensional array was used to load the data. However, when the number of items changes, such as dropping to 4, 3, 2, or even 1, the bars do not populate acc ...

There is no matching overload for this call in React Native

I am working on organizing the styles for elements in order to enhance readability. Here is the code I have written: let styles={ search:{ container:{ position:"absolute", top:0, }, } } After defining the s ...

What is the solution for the error "Build error occurred ReferenceError: self is not defined" when building a NextJs application?

pages/components/moru-sdk.js // https://libraries.io/npm/moru-web-sdk import { MoruCheckout } from "moru-web-sdk"; function MoruService() { const options = { access_key: "test_9425294388834bdface7d1b58fd538bf67627d9408fe4f258982 ...

Trying to showcase information received from a server using an API

For a school project, I am developing a website that can retrieve weather data (currently just temperature) based on a city or zip code from openweathermap.org using an asynchronous call. I have successfully retrieved the data from the API, but I am strug ...

REACT Issue: Unable to Select Dropdown Option with OnChange Function

I have a component that includes a select element. Upon clicking an option, the OnProductChange function returns the value. However, my e.target.value shows as [Object Object]. Yet, {console.log(product)} displays: {id: 1, name: "VAM"} Clicking on Add L ...

Error handling proves futile as Ajax upload continues to fail

Utilizing a frontend jQuery AJAX script, I am able to successfully transfer images onto a PHP backend script hosted by a Slim framework app. However, there is one specific image (attached) that is causing an issue. When the backend attempts to send back a ...

Python is experiencing difficulties with copying xpath elements

I attempted to utilize some Python code to access Twitter and retrieve the "Happening now" text. Unfortunately, it was unsuccessful. import webbrowser print("Visiting Twitter.com...") webbrowser.get('C:/Program Files (x86)/Google/Chrome/Application/c ...

Is there a way to have the headline gently fade in and subtly rise when the page loads using CSS?

Is it possible to make the headline fade in and move up slightly on the home page using CSS after the user lands on the website? For a visual reference, check out this example on . I know it can be achieved with translateY, but I have never tried it before ...

Inconsistency in date serialization using JSON.stringify across various web browsers

I've written this snippet in an HTML file: alert(JSON.stringify(new Date())); To cater to older browsers lacking JSON.stringify(), I've included the latest json2.js (2009-09-29 version) along with jquery-1.3.2.js. In modern browsers with native ...

Tips for utilizing the onload attribute alongside the ng-controller directive to run a function that has been established within the controller

When trying to call the submit() function as soon as the page loads, I keep encountering a submit() method not found error. The positioning of ng-controller and onload is confusing to me. If there is an alternate method in Angular to achieve this, please ...

Converting Node JS request to an API into React Fetch syntax

I have encountered a problem while developing an app in React-Native that connects with the Hubspot API. Initially, I tried to make the request using the Node JS request module, but it didn't work well with React Native when Expo was involved. Now, I ...

Error: The function $rootScope.$on is not defined within the Connectivity Factory

I am facing an issue with my Ionic app while trying to run it in offline mode. The error message "services.js:392 Uncaught TypeError: $rootScope.$on is not a function" appears when I launch the app. Can someone please explain why this is happening and what ...

Incorporate a vertical scrollbar in the tbody while keeping the thead fixed for smooth vertical scrolling

I'm seeking a solution to implement horizontal and vertical scroll for my table. Currently, the horizontal scroll is working fine, but when trying to add vertical scroll, the table header also moves along with it. Is there a way to keep the table hea ...

What is the best approach to creating DOM elements in AngularJS?

My controller contains data with various actions and different numbers of parameters: $scope.actions = [ {name : 'rotate', r : '30'}, {name : 'translate', x : '10', y : '10'}, {name : 'scale', ...

What is the best way to switch between light and dark themes with the ability to locally store the current theme?

Being new to the realm of react, I have been delving into the implementation of new features using Material-UI. One particular feature I am working on involves toggling between light and dark themes, with the current theme being stored locally within the b ...

React: Show input value on button click

I have been working on a React form that displays the entered input value in a controlled input element only after the user hits the submit button, rather than updating it constantly as the user types. Here is my current solution using conditional renderin ...

Transferring an ES6 class from Node.js to a browser environment

I've been exploring ways to bundle a super basic ES6-style class object for the browser. Here's an example: class Bar { constructor(){ this.title = "Bar"; } } module.exports = Bar; While I can easily utilize this in my node projec ...

Utilizing JavaScript and JSON to Retrieve Array Elements by Key Names as Indexes

I'm looking for a way to access elements in an array using unique key names instead of numerical indexes. Specifically, I'm developing a Discord bot where each server has its own settings. When a message is sent on a server, I need to retrieve th ...

Remove files from the server using an AJAX request

I am attempting to delete files on the SERVER using JavaScript, and I have already consulted the advice provided in this JavaScript file deletion thread My current JavaScript code looks like this: deleteFile = function() { return $.ajax({ url: "de ...

Performing a sequence of actions using Jquery Queue() function and iterating through each

I am facing an interesting challenge with an array called result[i]. My goal is to iterate through each field in the array and add it to a specific element on my webpage. $("tr:first").after(result[i]); However, I would like this process to happen with a ...