0

I've googled and gone through stack overflow Q&A's but haven't found this exact scenario. I have an object like so:

props: {
  "label": "1rem",
  "text3": "1rem",
  "text2Button": "1rem",
  "1": "1rem",
  "5spacing": 2
}

I am using this regex pattern to capture the object property names and remove the double quotes based on a simplified version of another answer:

/"([^"]+)":/g

This worked great, but I wanted the numbers to keep their double quotes, so I changed it to this:

/"([^"0-9]+)":/g

However, this only matches "label" and any property with a number is excluded. I understand why this is happening, what I can't figure out is how do I match properties that have a number in them but exclude those that begin with a number.

The desired regex pattern would match "label", "text3", and "text2Button" only so that I could then transform the object to this:

props: {
  label: "1rem",
  text3: "1rem",
  text2Button: "1rem",
  "1": "1rem",
  "5spacing": 2
}

(I couldn't figure out how to do combine the exclude ^ with the start of string ^. Nor could I figure out the proper place to add the | operator, or if that was even the correct approach.)

David Yeiser
  • 1,438
  • 5
  • 22
  • 33

2 Answers2

2

Your pattern doesn't work because [^"0-9]+ requires all the character to be neither " nor a digit while you only want to make sure the first character is not a digit.

You may use the following pattern:

/"([^0-9][^"]*)":/g

Demo.

1

Use this regex : Regex :

\"([a-zA-Z]\w*)\"(?=:)

enter image description here

Demo : Here

lagripe
  • 766
  • 6
  • 18
  • 1
    Note that this will not match `"a":`. You should replace `\w+` with `\w*`. – 41686d6564 stands w. Palestine Oct 14 '19 at 16:08
  • Thank you! This one works as well (with the single property name exception mentioned above). And I appreciate the diagram. I'm going to accept the other answer as it's closer to the original pattern of telling what to exclude rather than include. – David Yeiser Oct 14 '19 at 16:10