How to Extract a Number From a String in JavaScript

The JavaScript "indexof" function lets you identify a number in a string. You then assign the number to another variable you set up in the JavaScript function. This type of programming helps you separate numbers from letters in an alphanumeric string.

Instructions

    • 1

      Right-click the HTML file you want to edit. Click "Open With" in the pop-up menu, then click the program you use to edit JavaScript in Web pages.

    • 2

      Create the "script" tags that contain your JavaScript code. These tags denote JavaScript values, so the Web browser knows to run code instead of display text. Type the following code in the editor:

      <script type='text/javascript'> </script>

    • 3

      Type the following code in the script tags to extract the number from the JavaScript string:

      var number_from_string = oldstring.indexof('number');

      Replace "oldstring" with the name of the string that contains your number. Replace "number" with the number you want to extract.

Related Searches:

References

Comments

You May Also Like

  • How to Extract a Value From a Cell

    Open one of your HTML pages using Notepad and add this text to the document's "body" section: <table id="myTable" border="3"> <tr> <td...

Related Ads

Featured