0

I have a string that can contain any number of underscores many times in the string as following.

String: "Test_test__test_test__"

I need output like following for building SQL query.

String: "Test[_]test[__]test[_]test[__]"

I need to find a solution using Javascript or C#.

Thanks

Anup Kumar Gupta
  • 361
  • 5
  • 14
Maharshi
  • 1,178
  • 1
  • 14
  • 37
  • 2
    what have you tried so far? can you share the solution you have – guradio Jul 12 '17 at 05:30
  • One should ask a question or give an answer without downvoting as nothing was wrong with my question. – Maharshi Jul 12 '17 at 05:42
  • not my downvote but as it stands you want someone to do your homework for you – guradio Jul 12 '17 at 06:08
  • 1
    @Maharshi yes there is. If you google [c# replace](https://www.google.nl/?gws_rd=ssl#q=c%23+replace) you'll get your answer immediately. This question just shows lack of effort. – VDWWD Jul 12 '17 at 06:37

2 Answers2

3

Use String#replace method by specifying a string as a parameter in replace string.

var str = "Test_test__test_test__";

console.log(str.replace(/_+/g, '[$&]'));
Pranav C Balan
  • 113,687
  • 23
  • 165
  • 188
0

Use replace function

var str = "Test_test__test_test__";
console.log(str.replace(/_+/g,'[$&]'));
Dinesh undefined
  • 5,490
  • 2
  • 19
  • 40