A regular expression in JavaScript for matching unpredictable numbers without any specific words preceding them

For instance, I am looking to extract the numbers that do not have 'the', 'abc', or 'he' before them;

  1. the89 (no match)

  2. thisis90 (match 90)

  3. iknow89999 (match 89999)

  4. getthe984754 (no match)

  5. abc58934(no match)

  6. he759394 (no match)

I attempted to use this pattern (?<!(the|abc|he))[0-9]+, but it did not work as expected.

Answer №1

If you want to search using a regex pattern that includes negative lookbehind, you can try the following:

(?<!t?he|xyz|\d)\d+

Check out this RegEx Demo for more information

Here are the details of the Regex pattern:

  • (?<!: Start of a negative lookbehind that will fail if matched with:
    • t?he: Either "the" or "he"
    • |: OR
    • xyz: Exact match with "xyz"
    • |: OR
    • \d: Any digit
  • ): End of the negative lookbehind condition
  • \d+: Match one or more digits

Answer №2

If you want to extract numbers preceded by a whitespace, you can utilize the concept of positive lookbehind

/(?<=\s)\d+/g

const data = [
  "the89",
  "this is 90",
  "i like the60",
  "i know 89999",
  "get the984754",
];

const matches = data.map((s) => s.match(/(?<=\s)\d+/g));

console.log(matches);

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

Fetching information from the server in response to the data transmitted from the client

In need of help with sending a string id from the client to server side and retrieving related information using Node.js for the back-end. I have searched online but haven't found a solution yet. Hoping this isn't a redundant question. ...

A guide on importing gLTF files into three.js

I've been having some trouble uploading a gLTF file using three.js. Despite fixing some errors, I'm still greeted with a black screen. One solution was adding this to my chrome target directory: ‘path to your chrome installation\chrome.exe ...

In search of an effective method to divide a string based on words written in all capital letters

Suppose there is a random string given: var = 'I have a string I want GE and APPLES but nothing else' What is the most effective method to extract only 'GE' and 'APPLES' from the string in Python? In Java, I would split the ...

How to Display Prices in Euros Currency with Angular Filter

Can someone help me figure out how to display a price in euros without any fractions and with a dot every 3 digits? For example, I want the price 12350.30 to be shown as 12.350 €. I attempted to use the currency filter but it only worked for USD. Then ...

What is the process of exporting a module that has been imported in ES6?

Here are 3 code snippets: // ./src/index.ts const myApp = { test: 1, ... } export default myApp // ./src/app.ts import myApp from './src/index' export default myApp // ./index.vue import { example } from './src/app.ts' console.l ...

Utilizing Object-Oriented Programming in jQuery/JavaScript to effectively pass a value out of a function that is compatible across different browsers

While this question may have been asked before, I urgently need a solution for a production environment. I am feeling quite overwhelmed trying to figure out which objects I should create and utilize. function scroll(min, max) { // perform actions } fun ...

Following the submission of a message, the textarea automatically inserts a line-break

Can someone please help me troubleshoot an issue with my chat app? Every time I try to send a message, the textarea adds a line break instead of just focusing on the textarea so I can send a new message smoothly. I have recorded a video demonstrating the ...

My desire is for every circle to shift consecutively at various intervals using Javascript

I'm looking to draw circles in a sequential manner. I am trying to create an aimbooster game similar to . Instead of drawing all the circles at once, I want each circle to appear after a few seconds. The circles I've created currently do not g ...

Receiving an absence of string or a null value from a text box

My goal is to test the functionality of an <input> element by entering text into a textbox and then displaying that data in the console. However, I am encountering issues with retrieving and printing the content. Additionally, I am interested in test ...

Tips for automatically closing a Bootstrap 3 modal when AJAX request succeeds?

I'm trying to close a modal upon ajax success, but I'm encountering an issue. Here is my code snippet: Javascript success: function() { console.log("delete success"); $('#deleteContactModal').modal('hide'); $( "# ...

My ability to click() a button is working fine, but I am unable to view the innerHTML/length. What could be the issue? (NodeJS

Initially, my goal is to verify the existence of the modal and then proceed by clicking "continue". However, I am facing an issue where I can click continue without successfully determining if the modal exists in the first place. This occurs because when I ...

Submit a form using Ajax without having to reload the page

Seeking help for implementing an ajax form submission with four answer options to a question. The goal is to submit the form without reloading the page upon selecting an option. Below is the code I have been working on. Any suggestions or solutions are wel ...

Empty nested Map in POST request

I am currently working on a springboot application with a React/Typescript frontend. I have defined two interfaces and created an object based on these interfaces. export interface Order { customer_id: number; date: Date; total: number; sp ...

Incorporate a Flask variable into a webpage seamlessly without refreshing the page

I am facing a challenge in importing the variable test_data from Flask to my webpage without having to reload it. I have tried clicking a button, but haven't been successful so far. Any suggestions? Flask: @blueprint.route('/data_analysis' ...

PHP and AJAX: Combining Powers to Fetch Data

Greetings. I am currently in the process of creating a WordPress plugin that will manually send an email containing WooCommerce Order details to a specified supplier's email address. I am facing a challenge in understanding how to load data when a use ...

When updating the innerHTML attribute with a new value, what type of performance enhancements are implemented?

Looking to optimize updating the content of a DOM element called #mywriting, which contains a large HTML subtree with multiple paragraph elements. The goal is to update only small portions of the content regularly, while leaving the majority unchanged. Co ...

Dynamic Text Labels in Treemap Visualizations with Echarts

Is it possible to adjust the text size dynamically based on the size of a box in a treemap label? I haven't been able to find a way to do this in the documentation without hardcoding it. Click here for more information label: { fontSize: 16 ...

jquery is unable to locate text

UPDATE: I have recently implemented a function that calculates and displays the length of a certain element, but it currently requires user interaction to trigger it: function check() { alert($("#currentTechnicalPositions").html().length); } After s ...

Unidentified event listener

I am currently facing an issue where I keep getting the error message "addEventListerner of null" even though I have added a window.onload function. The page loads fine initially but then the error reoccurs, indicating that the eventListener is null. Str ...

Guide on incorporating d3 axis labels within <a> elements?

Issue at Hand: I am working with a specific scale var y = d3.scalePoint().domain(['a','b','c']).range([0,100]); I have successfully created an axis for this scale var y_axis = svg.append("g").attr("class", "axis").call(d3. ...