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 :
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.