Javascript replace not working for some reason - Stack Overflow

Trying to remove ST,+ from this string below. I've tried so many different ways but just can'

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 '));

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 '));
Share Improve this question asked Sep 2, 2015 at 13:40 thedoggydogthedoggydog 4691 gold badge4 silver badges11 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 8

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, ''); 
}

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, '');
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745670008a4639347.html

相关推荐

  • Javascript replace not working for some reason - Stack Overflow

    Trying to remove ST,+ from this string below. I've tried so many different ways but just can'

    21天前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信