Skip to main content

r/jquery

members
online

Help with .replace() issues Help with .replace() issues

Im having issues with using .replace().

Instead of just removing the 'partial' part of the string, it removes everything behind/rest of the string from its target point/string

//current string trying to replace() part of:
//1:1^3,4,|3:0^5,9,|7:1^17,3,4,|

//trying to replace:  3:0^5,9,| to just ''  (removal)
//but removes remainder of string, leaving just: 1:1^3,4,|

var removalPart1 =  $(this).prev().prev().prev().val();
var removalPart2 = $(this).prev().prev().val();
var removalPart3 = $(this).prev().val();
var stringToRemove = removalPart1 + ":" + removalPart2 + removalPart3 + '|';

console.log("String to remove: " + stringToRemove);//displays: 3:0^5,9,|<--- which is correct

var updatedString = currentString.replace(stringToRemove, "");            

// Update the input field's value
$("#currentField").val(updatedString);

Im not clear as to 'why' this is happening though? Not using Regex? (but could it be that : ^ , are part of the string/removal?)

Appreciate feedback

Thanks!


Norman is back to being part of the action—and back to his old self. With once-monthly Librela (bedinvetmab injection), Norman’s osteoarthritis (OA) pain is under control. See more of Norman’s real results with Librela at Librela.com.

Please see Prescribing Information for Librela.

media poster