How to use Regular Expression Directly In Javascript

This approach directly employs a regular expression to match numeric and decimal characters in the currency string. It then extracts these characters, joins them to form a numeric string, and converts it to a double value using parseFloat().

JavaScript
// Function to convert currency string to double using regular expression
function convertCurrencyToDouble(currency) {
    // Using regular expression to match numeric and decimal characters
    const regex = /[\d.]+/g;
    // Extracting numeric and decimal characters from the currency string
    const matches = currency.match(regex);
    // Joining the matched characters to form a numeric string
    const numericString = matches.join('');
    // Converting the numeric string to a double value
    const doubleValue = parseFloat(numericString);
    return doubleValue;
}

// Driver code

// Currency in string
let stringCurrency = "$6542.45";
console.log("Currency value: " + stringCurrency);

// Converting currency
let doubleValue = convertCurrencyToDouble(stringCurrency);

// Display currency
console.log("Converted to double: " + doubleValue);

// Currency in string
stringCurrency = "$357,545.45";
console.log("Currency value: " + stringCurrency);

// Converting currency
doubleValue = convertCurrencyToDouble(stringCurrency);

// Display currency
console.log("Converted to double: " + doubleValue);

Output
Currency value: $6542.45
Converted to double: 6542.45
Currency value: $357,545.45
Converted to double: 357545.45




How to convert a currency string to a double value with jQuery or Javascript?

In this article, we will convert a currency string to a double value with jQuery or Javascript. There are two popular ways to convert currency string into float string using different JavaScript inbuilt libraries. 

Similar Reads

Methods to convert Currency String to a Double value

Table of Content Method 1: Using str.charCodeAt() and substring() method Method 2: Using JavaScript replace and parseFloat methodsMethod 3: Using Regular Expression Directly...

Method 1: Using str.charCodeAt() and substring() method

This is a simple approach in which we will match characters one by one from starting using the loop and if any character comes out to be of integer type or numeric type then we will use the substring() method to take out the substring from the original string. After taking out the substring we will use the parseFloat() method to convert that string to float to a double value. To check whether the character is of numeric type or not, we use charCodeAt() method to get the Unicode value of the specified character....

Method 2: Using JavaScript replace and parseFloat methods

In this method, we will use replace()method of JavaScript to replace every character other than numbers and decimal (.) with blank (“”). After then we can use parseFloat() method to make the string float or double. This method is more efficient than method 1....

Method 3: Using Regular Expression Directly

This approach directly employs a regular expression to match numeric and decimal characters in the currency string. It then extracts these characters, joins them to form a numeric string, and converts it to a double value using parseFloat()....

Contact Us