Regular Expression - Locate all numerical values within text formatted with HTML

I am attempting to locate all the numbers within an HTML document. However, I want to ensure that I exclude numbers that are part of a word, such as "o365", "high5", and similar instances.

Here is my current approach, but it does not successfully avoid words:

regular expression:

[\s+>][-.0-9]+

sample HTML snippet:

<p ng-if="e.element != 'attachment'" ng-bind-html="::e.value" class="ng-binding ng-scope">123 Hello need 123 help with 0365 thanks</p>

Answer №1

One option is to utilize a straightforward regex pattern:

\b\d+\b

Explore demo here

The concept revolves around identifying digits within boundaries

Answer №2

When using a dot in your sample regexp, it appears that you are attempting to retrieve both floating point numbers and integers indiscriminately. To account for the sign, an optional sign should be considered first:

[+-]?

Following this, there must be a sequence of digits (at least one):

[0-9][0-9]*

(this can also be written as \d+) next, optionally, a dot followed by another sequence of digits (which may be empty)

(\.\d*)?

Furthermore, if you want to ensure these numbers are not attached to alphabetic input, word boundaries need to be placed on both ends. Therefore, the final regex would look like:

\b[+-]?\d+(\.\d*)?\b

As demonstrated in demo.

The demo showcases three unusual cases which warrant attention:

  1. The right boundary avoids matching +15350.16f, capturing only +15350. The dot is recognized as a boundary, however, since it's a valid number, we exclude the right boundary.
  2. In this instance, the + sign functions as a nonword character, creating a left-side word boundary to correctly scan.
  3. In this case, due to the left boundary, we need to skip the initial part of the number (e25). The dot acts as a word boundary for the fractional part, allowing 42 to be scanned as a number after the dot. This scenario seems complex; additional context might be required to address this situation.

To mitigate the last case, context needs to be added prior to our number, determining whether to accept or reject the number based on that context. If something matches within the first group, everything is discarded; hence:

([a-zA-Z]?)

When appended to our regexp:

([a-zA-Z]?)([+-][0-9]+(\.[0-9]+)?)

In such cases, rejection occurs if group 1 has any matches. Conversely, if group 1 is empty, the number from group 2 is obtained. Refer to demo2.

The demo illustrates that a letter connected to a signed number could potentially be valid, resulting in match rejection due to the presence of the letter in the first group. To prevent this, two regular expressions will be _or_ed together to form two alternatives: first without a sign included:

([a-zA-Z]?)([0-9]+(\.[0-9]*)?)

Followed by the signed original expression (sign being mandatory in this case).

([+-][0-9]+(\.[0-9]*)?)

Therefore, if group 1 contains anything, the expression is rejected as not being a valid number. Group 2 indicates an *unsigned floating point or integer number*, while group 4 represents a *signed floating point or integer number*. The final regexp is:

([a-zA-Z]?)([0-9]+(\.[0-9]*)?)|([+-][0-9]+(\.[0-9]*)?)

Refer to demo3.

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

developing a dynamic map with javascript

In the image provided, my concept is for it to initially be without a green tint. However, when the user hovers over specific areas, they would turn green and become clickable links. There are multiple areas in the image, with this being just one example. ...

Tips on activating the CSS style while typing using the onChange event in React

Is it possible to dynamically adjust the width of an input as we type in React? Currently, my input has a default width of 1ch. I would like it to increase or decrease based on the number of characters entered, so that the percentage sign stays at the end ...

Encountering an issue... invariant.js:42 Error: A `string` value was received instead of a function for the `onClick` listener

Currently, I am working on creating a form that allows users to build quizzes without any restrictions on the number of questions they must include. To achieve this, I plan to add an "add question" button at the bottom of the quiz form, allowing users to d ...

Which method is better for presenting data: PHP or JavaScript?

Currently, I am diving into vue.js on laracasts.com where Jeffrey Way demonstrates 2 ways to showcase data on a webpage. One method involves displaying data using Laravel foreach loops, while the other utilizes vue.js. This has led me to ponder: is there ...

Exploring the wonders of accessing POST request body in an Express server using TypeScript and Webpack

I am currently working on a Node and Express web server setup that utilizes Webpack, along with babel-loader and ts-loader. Let's take a look at some key portions of the code: webpack-config.js: const path = require("path"); const nodeExte ...

A tutorial on submitting multipart/form-data using JavaScript

I am currently facing an issue with the uploadImage API in my frontend. I am using input[type='file'] to obtain image data from users and then POSTing it to my API. In the backend, I am parsing this data using formidable. However, the problem ari ...

Unable to access account due to login function malfunctioning

I've created a login button with this function code, but I keep getting the error message "Login unsuccessful, Please try again..." Thank you in advance. I wasn't entirely sure what you needed, so I included most of the code because I've b ...

Counter is effective for the initial post, yet it does not function properly for the subsequent post

Can anyone help with an issue I'm having with my JavaScript counter? It works for the first comment, but not the second one. This problem persists whether I use PHP or JavaScript. Below is the JavaScript code for the counter: var count = (function() ...

Manipulating data with Angular's array object

I am having an issue with posting an object array. I anticipate the post to be in JSON format like this: {"campaign":"ben", "slots":[ { "base_image": "base64 code here" } ] } However, when I attempt to post ...

Ways to shift text upwards while scrolling in a downward direction?

I'm a beginner with jQuery and I could use some assistance. My goal is to have the text move upwards and a static box slowly disappear as you scroll down the website. Something similar to what you see here: p, body { margin: 0; padding: 0; } b ...

Creating dynamic and asynchronous JSON structures with JavaScript

Struggling with async JavaScript here. I've got a function called within a jQuery AJAX function, and it looks like there are more async method calls in that function. Currently stuck in this situation. Here's the code snippet triggered by the jQ ...

Why does the getComputedStyle function return null for IE11 and Edge when using Kendo React Grid within a Popout window, while it works fine in Chrome, Opera, and Firefox?

Check out my stackblitz demo where I am experimenting with rendering a Kendo React Grid inside a popout window using the react-popout-component. The demo runs smoothly in Chrome, Opera, and Firefox, but encounters issues in Edge and IE11 due to a null valu ...

I attempted to access data from the phpmyadmin database, but the browser is displaying an error message stating "cannot get/employee", while there are no errors showing in the command prompt

const { json } = require('express/lib/response'); const mysql=require ('mysql'); const express=require('express'); var app=express(); const bodyparser=require('body-parser'); app.use(bodyparser.json()); var mysq ...

Error: The object #<HTMLCollection> does not support the 'tags' method

While trying to troubleshoot this issue, it seems like I haven't been able to pinpoint the simple solution. This particular javascript menu works perfectly in IE, however in Chrome, there is an error when trying to open the menu with a first-level cli ...

Issues are arising with Jquery Scripts when running through Selenium in IE 9, resulting in the error message SCRIPT5009: '$' is undefined. However, these scripts are functioning correctly when executed directly in the web browser

While using Selenium, the code below is causing issues as it is not functioning properly. An error SCRIPT5009: '$' is undefined is being thrown in IE 9. However, if the code is executed in a web browser console after removing the "\" sign, i ...

Is dynamic data supported by Next.js SSG?

I'm currently developing a web application with Next.js and I need clarification on how Static generated sites work. My project is a blog that necessitates a unique path for each blog entry in the database. If I were to statically generate my web appl ...

Is there a way to sequentially load two iframes, with the second one loading only after the first one is fully loaded

Having two different links that trigger ajax calls can be tricky if only one request is allowed per load. This may result in both iframes displaying the same data. Is there a way to work around this issue? Perhaps loading one iframe first and then triggeri ...

Is there a way to modify this within a constructor once the item has been chosen from a randomly generated array?

If I use the following code: card01.state = 3; console.log(card01); I can modify the state, but I'm interested in updating the state of the card chosen by the random function. class Item { constructor(name, state) { this.name = name; thi ...

Developing Authorization in AngularJS

Incorporating authorization into an AngularJS project is crucial. In my current project, which revolves around a social media concept, users with different roles may have varied access to view files. For instance, envision two distinct roles: customer and ...

Send information using AJAX within a .NET integrated browser

In our current setup, we utilize a .NET embedded browser to showcase an HTML page that is generated by applying an XSLT file to an XML file using .NET. This process results in HTML content displayed within the embedded browser through the DocumentText prop ...