I want to use str_replace or its similar alternative to replace some text in JavaScript.
str_replace
var text = \"this is some sample text that i want to re
that function replaces only one occurrence.. if you need to replace multiple occurrences you should try this function: http://phpjs.org/functions/str_replace:527
Not necessarily. see the Hans Kesting answer:
city_name = city_name.replace(/ /gi,'_');