0

so here is the code

var searchValue = "Dans-Test";

if ( searchValue.indexOf('-') >= 0 ){
searchValue.replace('-', ' ');  
console.log("reached here");
} else {
console.log("no -");
}

console.log(searchValue);

searchValue is still outputting Dan-Test and not Dan Test, why?

it does console log reached here so the if statement is correct

thanks

4

2 回答 2

5

replace doesn't modify the original string, it's non-mutable:

searchValue = searchValue.replace('-', '');
于 2013-04-10T08:31:33.833 回答
0

Additionally, the replace function only replace the first matched character.

于 2013-04-10T08:34:54.500 回答