Ahsan Ali Ahsan Ali - 3 months ago 6
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"

"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(' ');