10 examples of 'extract number from string javascript' in JavaScript

Every line of 'extract number from string javascript' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
5function extractNumber(string) {
6 if (!regex.test(string)) {
7 console.error('Imagemagick\'s compare method failed');
8 console.error(string);
9 return Infinity;
10 }
11
12 var match = string.match(regex);
13 return +match[1];
14}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

259extractNumber() {
260 let char = this.source[this.cPointer];
261 let number = "";
262 while (/[\d\.]/.test(char)) {
263 number += char;
264 char = this.source[++this.cPointer];
265 }
266 return parseFloat(number);
267}
42function getNumber (str) {
43 str = str.replace(/(invert)|(brightness)|(blur)|(contrast)|(grayscale)|(sepia)|(threshold)|(gamma)?\(/g, '').replace(')', '')
44 if (str.indexOf('%') !== -1) {
45 return Number(str.replace('%', '')) / 100
46 } else if (str.indexOf('px') !== -1) {
47 return Number(str.replace('px', ''))
48 } else {
49 return Number(str)
50 }
51}
212function extractNumber(v) {
213 return parseFloat(replace(v, /^[^\d-]+/));
214}
1function number(input: string | number): number {
2 if (typeof input === 'string') {
3 return parseInt(input.replace(/^\D+/g, ''), 10);
4 }
5
6 return 0;
7}
15function getNumFromStr(str) {
16 return `${str.match(/\d+/)[0]}`;
17}
15function getNumbers(string) {
16 if (isUndefined(string) || !isString(string)) {
17 return '';
18 }
19
20 return String(string).replace(/\D+/g, '');
21}
706value: function extract(string, format, formatChunk) {
707 var ignoreCase = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : true;
708
709 var targetFormat = ignoreCase ? format.toLowerCase() : format;
710 if (targetFormat.includes(formatChunk)) {
711 return parseInt(string.substr(targetFormat.indexOf(formatChunk), formatChunk.length));
712 }
713}
99var parseNumber = function parseNumber(string) {
100 return (string.match(numberAccept) || []).join('');
101};
8export function parseNumber(s) {
9 s = String(s).replace(/[^\d-.]/g, '').replace(/\.$/, '');
10 if (!s.match(NUMBER_RE)) { return null; }
11 return parseFloat(s, 10);
12}

Related snippets