How to replace String value with regex

76 views Asked by At

MOrris Example

Morris.Bar({
  element: 'bar-example',
  data: [
    { month: "2006", amount: 100},
    { month: "2007", amount: 75}
  ],
  xkey: "month",
  ykeys: ["amount"],
  labels: ["amount"]
});

amount in ykeys and lables should be same as it is mentioned in Morris Example. Otherwise graph would not display because of error in amount format.

At moment my amount value is in this way

String amount = "[amount]";

amount="[amount]"

and i want value in this way

["amount"]

What would be easiest and preferred way to replace these values?

2

There are 2 answers

6
Azodious On

Why regex?

If you can achieve your task with simple calls to APIs which don't require regex, stick to them.

amount.subString(1,amount.length()-1);
1
Mark W On

You don't need to use regex.

  amount = amount.replace("[", "[\"").replace("]","\"]");

Edited. OP cleared up what was needed. The above code is replacing [ with [" and ] with "].

The " is escaped within java with \