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

1
  • 1
    Returns a new string with some or all matches of a pattern replaced by a replacement. The pattern can be a string or a RegExp, and the replacement can be a string or a function to be called for each match. developer.mozilla.org/en/docs/JavaScript/Reference/… Commented Apr 10, 2013 at 8:32

2 Answers 2

5

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

searchValue = searchValue.replace('-', '');
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.