whreed
whreed

Reputation: 143

Regex Get Rid of Hyphens Between Quotes

I get a json string back from a device, the string has hyphens in the defintion names and I want to remove that...

Current I have

\"(.+?)\":

But that gets everything within "": I want only the hyphen in there, not all the text. I know I am close just having trouble because regex always confuses me. Below I would like to correct serial-number to serialnumber but not value-2....help!

{
  "result": {
    "Response": {
      "info": {
        "serial-number": "xyz",
        "value1": "value-2",

Upvotes: 0

Views: 88

Answers (1)

Chris Lear
Chris Lear

Reputation: 6742

You can match

/"([^"]*)-([^"]*)":/

And then replace with just the two submatches. See http://www.regexpal.com/?fam=95143

This is the sort of js code that should work:

json = json.replace(/"([^"]*)-([^"]*)":/g,'"$1$2"');

Here's another version, using a lookahead:

json = json.replace(/-(?=[^"]*":)/g,"");

This assumes there's never a space between the closing " and the :

Upvotes: 1

Related Questions