Home > front end >  JavaScript choosing a specific type of number in a list
JavaScript choosing a specific type of number in a list

Time:12-31

I'm trying to choose specific type of number among a list of numbers in JavaScript. a Console.log(vars) outputs the list of numbers below. And I need to choose the numbers that have the 0.000 format, meaning at least one number before decimal and three after the decimal point.

(66) ['47,416.00', '0.001', '08:17:36', '47,416.00', '0.005', '08:17:36', '47,416.00', '0.088', '08:17:36', '47,416.00', '0.003', '08:17:35', '47,416.00', '0.214', '08:17:33', '47,416.00', '0.005', '08:17:33', '47,416.00', '0.008', '08:17:33', '47,416.00', '0.005', '08:17:33', '47,416.00', '0.010', '08:17:33', '47,416.00', '0.758', '08:17:33', '47,416.00', '', '0.134', '08:17:33', '47,416.50', '', '0.004', '08:17:33', '47,416.00', '0.018', '08:17:31', '47,416.00', '0.171', '08:17:30', '47,416.00', '0.034', '08:17:28', '47,416.00', '', '0.010', '08:17:28', '47,416.50', '0.003', '08:17:28', '47,416.50', '1.000', '08:17:28', '47,416.50', '0.011', '08:17:28', '47,416.50', '0.001', '08:17:25', '47,416.50', '0.005', '08:17:25']

I tried a regex and you can see it in my code below but regex is only returning true.

var tradescards = [];
let regex = /\d\.\d\d\d/;


function obser1() {
    const trades = document.querySelector("#root > main > div.main-left > div.react-grid-layout > div:nth-child(3) > div > div.by-card__body > div > div");
    const vars = [...trades.querySelectorAll("span")].map(span => span.textContent);
    const observer = new MutationObserver(function (mutations) {
        mutations.forEach(function (mutation) {
            if (mutation.addedNodes.length) {
                console.log(regex.test(vars));
            }
        })
    });
    observer.observe(trades, {
        childList: true,
        subtree: true,
        attributes: true,
        characterData: true
    })
}
window.setTimeout(obser1, 1900);

CodePudding user response:

arr.filter(e => /\d.*\.\d\d\d\b/g.test(e))

You are looking for something like this.

There's a post explaining working of Regex.

Regex Matches - Wildcard then one decimal place

  • Related