Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Remove integers in the beginning of a string in jquery

I have text in a table’s TD which may start with space or number or multiple spaces. I need to remove all the spaces and numbers until I hit a character. I need to do it in jQuery. I am trying to split the text based on the spaces and remove them. But not working.

$(document).ready(function () {
    $("table tr").each(function () {
        var leftText = $(this).find("td.leftLinesTD").text();
        var i = 0;
        leftText.split(" ").every(v => {
            if (v = '') {
              leftText.replace(leftText.split(" ")[i], '');
              i = i + 1;
              return true;
            }
            else { return false; }
        });
     });

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

This is more of a JavaScript & strings question than it is a jQuery question.

You would benefit from use of a regular expression:

let leftText = $(this).find("td.leftLinesTD").text();
leftText = leftText.replace(/^[\d\s]*/, '');

This regular expression says "from the start of the string (^), gather zero-or-more (*) instances of digits (\d) and whitespace (\s) and replace the matched substring with an empty string ('')".

BTW: use of var is discouraged these days. Use of let and const are more appropriate.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading