Select numbers on a page with jQuery or Javascript

Viewed 2679

I'm just wondering if there's a way to locate numbers on a page with jQuery or plain Javascript.

Here's what I want to do:

Say "June 23" is on the page. What I want to do is be able to prepend and append some <span> selectors to the number.

Using :contains() with jQuery selects the whole thing, not just the number.

These strings are being generated without any wrapping elements by a Wordpress theme I'm working on, and I only want to select the number.

Any help would be appreciated! Thanks for even thinking about it.
-George

4 Answers

You can also select decimal numbers that contain comma as thousands separators:

let regex = /([,\d]*\.?\d+)/g;

This will match 1234 and 1,234 and 1234.5678 and 1,234.5678 and 0.5678 and .5678.

Refer to the above answer for full solution.

Related