46795

how to extract numbers from string using Javascript?

Question:

how to extract numbers from string using Javascript? Test cases below:

string s = 1AA11111 string s1= 2111CA2 string s result = 111111 string s1 result = 211112

My code below is not working:

var j = 0; var startPlateNums = ""; while (j <= iStartLength && !isNaN(iStartNumber.substring(j, j + 1))){ startPlateNums = startPlateNums + iStartNumber.substring(j, j + 1); j++; }

Answer1:

How about a simple regexp

s.replace(/[^\d]/g, '')

or as stated in the comments

s.replace(/\D/g, '')

<a href="http://jsfiddle.net/2mguE/" rel="nofollow">http://jsfiddle.net/2mguE/</a>

Answer2:

You could do: <strong>EDITED:</strong>

var num = "123asas2342a".match(/[\d]+/g) console.log(num); // THIS SHOULD PRINT ["123","2342"]

Answer3:

A regex replace would probably the easiest and best way to do it:

'2111CA2'.replace(/\D/g, '');

However here's another alternative without using regular expressions:

var s = [].filter.call('2111CA2', function (char) { return !isNaN(+char); }).join('');

Or with a simple for loop:

var input = '2111CA2', i = 0, len = input.length, nums = [], num; for (; i < len; i++) { num = +input[i]; !isNaN(num) && nums.push(num); } console.log(nums.join(''));

Recommend

  • What is the purpose of Double.IsNaN(double d)? [closed]
  • Easy way to convert a string to a number or null?
  • Removing entire rows with NaNs from a multidimensional array in Julia?
  • GNU sed, ^ and $ with | when first/last character matches
  • Get the elements of HTML tags inside data-content of popover
  • jQuery & CSS - Cut text by height, no truncate
  • Strange behaviour in Material datepicker
  • How to select dynamically in Bootstrap-select with multiple values
  • jQuery panel slider opens with button click but won't close
  • Convert array of strings to array of objects
  • Jquery Mobile + Phonegap improve listview performance
  • How can I have a scrollable body with fixed header and footer inside a container div?
  • Does Perl currently (5.8 and 5.10) make any promises about the order alternations will be used?
  • pyspark substring and aggregation
  • Multiple jQuery Mobile Count Bubbles
  • Jquery replacing text inside html
  • knockout error - Uncaught Error: Unable to parse bindings.
  • Chrome firing onClick when space typed in contenteditable button
  • Sticky Footer W/JQuery Mobile
  • formediting modals shows at wrong position
  • Collapsible Sankey Diagram - D3
  • Jquery resizable reposition handle after scroll
  • Is there a way to clone native functions in javascript like window.alert or document.write
  • Replace last two characters in column
  • cell spacing in div table
  • Django simple Captcha “No module named fields” error
  • Could not find rake using whenever rails
  • How to clear text inside text field when radio button is select
  • Why ng-show works with ng-repeat but ng-if doesn't? [duplicate]
  • Circular dependency while pushing http interceptor
  • How to get icons for entities from eclipse?
  • Linker errors when using intrinsic function via function pointer
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • Can Visual Studio XAML designer handle font family names with spaces as a resource?
  • Turn off referential integrity in Derby? is it possible?
  • JaxB to read class hierarchy
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • UserPrincipal.Current returns apppool on IIS
  • java string with new operator and a literal