Get a Number from a String using JavaScriptIn this article, we will understand how to get a number from String utilizing JavaScript. We can only get those numbers that are present in the provided string. There are various methods by which we can get a number from String, which are as follows:
We will understand all methods to get a number from String using JavaScript one by one. JavaScript match method with regExWe can get a number from a string with the help of match() method. This method takes the regEx value as an input. The regEx means the regular expression that describes the pattern of a string and permits finding all kinds of text such as email ID, password, etc. It can be utilized for validating purposes like validating email text. Syntax: str.match() is the method in the above-given syntax. The (/(\d+)/) is the function value which is actually the regular expression that we will utilize to get the number from a string. The \d is the metacharacter which is utilized to match the digits from 0 to 9. Demonstration-1:We are extracting a number from a given string in the following demo. We will utilize the match() function to match the string and regular expression, i.e., (/(\d+)/) to match the specific string. Code: Output: Here is the output in which we can witness a number that is extracted from a string. Demonstration-2:We will get all numbers available in the given string. We will utilize the match() function to match the string and regular expression, i.e., (/(\d+)/g) to match the specific string. The g in the regEx is utilized for global search that permits finding all numbers present in the string. Code: Output: We can witness each extracted number in the outcome provided below. JavaScript reduce() method with the spread operatorarr.reduce()The arr.reduce() is the method that is utilized to reduce the array to one value. Syntax: The function(total, currentValue, currentIndex, arr) and initialValue are the parameters that the arr.reduce() function takes. They are described below:
Spread operatorThe spread operator (…) is utilized to copy an existing array into another array. It gets numbers from an array. Syntax: Demo:We will get a number from a given string utilizing the arr.reduce() function with the spread operator. Code: Output: Here is the outcome where we can witness a number that is extracted from a string utilizing the reduce() method. JavaScript replace() method with regExThe replace()method is utilized to replace some portion of the given string with another string. It is essential to recall that the original string will not alter. Syntax: The arguments of the replace() method are value1 and value2. The value1 is the regEx which is the string pattern that has to be replaced and value2 is the value of the string which replaces part of the given string. Demonstration-1:We are going to form a JavaScript code to take out a number from a string in this demo by utilizing the replace() method with the assistance of the regEx. Code: Output: Here in the output downwards we can witness an extracted number from a string utilizing the replace() method. Demonstration-2:We will build a JavaScript program to get a decimal number from a provided string in this demo by utilizing the replace() function with the regEx. Code: Output: Here in the output we can clearly witness a decimal number extracted from a given string utilizing the replace() method. Conclusion:We have comprehended how to get a number from a string using JavaScript. We have understood the ways to extract a number from a string with the help of demonstrations. The ways to get a number are utilizing the JavaScript match method with regEx, utilizing the JavaScript reduce() method with the spread operator and utilizing the JavaScript replace() method with regEx. Next TopicJavaScript Map VS Object |