Skip to main content
added 216 characters in body
Source Link
Christophe
  • 28.3k
  • 29
  • 105
  • 145

Chaining is cool, why dismiss it?

Anyway, here is another option in one replace:

string.replace(/#|_/g,function(match) {return (match=="#")?"":" ";}) 

The replace will choose "" if match=="#", " " if not.

[Update] For a more generic solution, you could store your replacement strings in an object:

var replaceChars={ "#":"" , "_":" " }; string.replace(/#|_/g,function(match) {return replaceChars[match];}) 

Chaining is cool, why dismiss it?

Anyway, here is another option in one replace:

string.replace(/#|_/g,function(match) {return (match=="#")?"":" ";}) 

The replace will choose "" if match=="#", " " if not.

Chaining is cool, why dismiss it?

Anyway, here is another option in one replace:

string.replace(/#|_/g,function(match) {return (match=="#")?"":" ";}) 

The replace will choose "" if match=="#", " " if not.

[Update] For a more generic solution, you could store your replacement strings in an object:

var replaceChars={ "#":"" , "_":" " }; string.replace(/#|_/g,function(match) {return replaceChars[match];}) 
Source Link
Christophe
  • 28.3k
  • 29
  • 105
  • 145

Chaining is cool, why dismiss it?

Anyway, here is another option in one replace:

string.replace(/#|_/g,function(match) {return (match=="#")?"":" ";}) 

The replace will choose "" if match=="#", " " if not.