Example: Want to extract the price, could be an integer or float from a dataset of prices.
$55.99 $55 $55.00 etc...
You can use a Regular Expression tool such as the Firefox Add-on: Regular Expressions Tester.
As you can see in tool, the matched expression is highlighted yellow.
var price = '$55.99'; var priceRegex = /[(0-9)+.?(0-9)*]+/igm; console.log(priceRegex.test(price)); console.log(price.match(priceRegex)); console.dir(priceRegex.exec(price));
Now if we perform some basic JS tests using .test(), match() and exec() this is what we get.
The exec() function result at index 0 gives us our match so the full code for extracting the price.
var price = '$55.99'; var priceRegex = /[(0-9)+.?(0-9)*]+/igm; var price = parseFloat(priceRegex.exec(price)); console.log(price); //output: 55.99
Sam Deering has 15+ years of programming and website development experience. He was a website consultant at Console, ABC News, Flight Centre, Sapient Nitro, and the QLD Government and runs a tech blog with over 1 million views per month. Currently, Sam is the Founder of Crypto News, Australia.
Jump Start Git, 2nd Edition
Visual Studio Code: End-to-End Editing and Debugging Tools for Web Developers