Ahsan Ali Ahsan Ali - 1 year ago 70
Javascript Question

Replace text in javascript, jquery

var input="gazanie, flower, blossom";

I need to replace var text using JavaScript or jQuery.

Output should be like:

"#gazanie #flower #blossom"

Answer Source
"gazanie, flower, blossom".split(',').map((e)=>`#${e.trim()}`).join('');

THat's it :

  • split : to split string according to delimitor which is , & get array
  • map : to add prefix for each element which is #
  • trim : eliminate space if it is the 1st char or/and is the last char.
  • join : to return back from Array to String .


var input="gazanie, flower, blossom";

var output=input.split(',').map((e)=>`#${e.trim()}`).join(' ');

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download