thedoggydog
thedoggydog

Reputation: 469

Javascript replace not working for some reason

Trying to remove ST,+ from this string below. I've tried so many different ways but just can't seem to get it to remove anything from the string at all. Anything I'm doing wrong?

function convertSerialData(valueIn){

valueIn.replace(/ST/i, ''); 

return valueIn;
}

alert(convertSerialData('ST,+00.8  g '));

Upvotes: 1

Views: 494

Answers (2)

gen_Eric
gen_Eric

Reputation: 227200

valueIn.replace(/ST/i, ''); doesn't modify the string, it returns a new one. You need to use the value returned from the .replace() function.

Also, if you want to remove more than just ST, then you just need to update your regex to remove the characters you need.

function convertSerialData(valueIn){
    return valueIn.replace(/ST,\+/i, '');
}

Upvotes: 6

Michael McMullin
Michael McMullin

Reputation: 1460

You don't seem to be assigning the results of the replace:

valueIn = valueIn.replace(/ST,[+]/i, '');

Or, more concisely:

function convertSerialData(valueIn){
    return valueIn.replace(/ST,[+]/i, ''); 
}

Upvotes: 8

Related Questions