Extract a Quantity from a String with JavaScript


Consumer enter from HTML type fields is usually supplied to JavaScript as a string. We have lived with that reality for many years however generally builders must extract numbers from that string. There are a number of methods to get these numbers however let’s depend on common expressions to extract these numbers!

To make use of an everyday expression to get a quantity inside a string, we are able to use d+:

const string = "x12345david";
const [match] = string.match(/(d+)/);
match; // 12345

Common expressions are able to actually highly effective operations inside JavaScript; this apply is without doubt one of the simpler operations. Changing the quantity utilizing a Quantity() wrapper offers you the quantity as a Quantity kind.

  • Being a Dev Dad

    I get requested a great deal of questions daily however I am all the time shocked that they are hardly ever questions on code and even tech — most of the questions I get are extra about non-dev stuff like what my workplace is like, what software program I take advantage of, and oftentimes…

  • Welcome to My New Office

    My first skilled net improvement was at a small print store the place I sat in a windowless cubical all day. I suffered that boxed in setting for nearly 5 years earlier than I used to be capable of finding a distant job the place I labored from residence. The primary…

  • Better Pull Quotes with MooTools
  • Highlight Table Rows, Columns, and Cells Using MooTools 1.2.3

    Row highlighting and particular person cell highlighting in tables is fairly easy in each browser that helps :hover on all parts (principally all the pieces besides IE6). Column highlighting is a little more troublesome. Fortunately MooTools 1.2.3 makes the method simple. The XHTML A traditional desk. The cells…


Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles