javascript - Replace multiple characters in one replace call -


very simple little question, don't quite understand how it.

i need replace every instance of '_' space, , every instance of '#' nothing/empty.

var string = '#please send_an_information_pack_to_the_following_address:';

i've tried this:

string.replace('#','').replace('_', ' ');

i don't chaining commands this, there way in one?

shannon

use or operator (|):

var str = '#this #is__ __#a test###__'; str.replace(/#|_/g,''); // result: "this test" 

you use character class:

str.replace(/[#_]/g,''); 

fiddle

if want replace hash 1 thing , underscore another, have chain. however, add prototype:

string.prototype.allreplace = function(obj) {     var retstr = this;     (var x in obj) {         retstr = retstr.replace(new regexp(x, 'g'), obj[x]);     }     return retstr; };  console.log('aabbaabbcc'.allreplace({'a': 'h', 'b': 'o'})); // console.log 'hhoohhoocc'; 

why not chain, though? see nothing wrong that.


Comments

Popular posts from this blog

Java 3D LWJGL collision -

spring - SubProtocolWebSocketHandler - No handlers -

methods - python can't use function in submodule -