Tags
Asked 3 years ago
16 Jun 2021
Views 235
Taryn

Taryn posted

how can get only numbers from the text using jquery

how can get only numbers from the text using jquery
sqltreat

sqltreat
answered Apr 27 '23 00:00

You can extract only numbers from a string in jQuery by using regular expressions and the replace() method. Here's an example:



var text = "The price of the item is $20.99";
var number = text.replace(/[^0-9]/g, '');
console.log(number); // Output: 2099

In this example, we have a string variable text that contains a sentence with a price value of $20.99. We use the replace() method to replace all non-numeric characters with an empty string. The regular expression /[^0-9]/g matches all characters that are not in the range of 0-9, and the g flag is used to match all occurrences of the regular expression in the string.

The resulting value of number will only contain the numbers extracted from the string, which in this case is the value 2099.

You can adjust the regular expression to match different patterns of numbers, depending on your specific requirements.
Post Answer