fr0styy fr0styy - 5 months ago 9
JSON Question

Replace with unknown character

I am correcting a json-Array. I want to replace a few errors.

For Example: in

"index" : NumberInt(8),
i want to cut off
NumberInt(*)
without the number where the * is, in order to make the json-file valid.

How can i do that? didn't find anything on google. it's quite hard to define this question.

Example

Before:


"index" : NumberInt(8),


(Some way of changing the JSON)

After:


"index" : 8,





Edit:

after the marked answer i could figure out my specific case by myself.

I solved my provlem using the "Back-References" ($1, $2, etc)

Example, which i used for my case:


press cmd+R -> replace function

insert in Search-String:
NumberInt\(+(\d)\)
insert in
Replace-String:
$1


what happens: it searches for "NumberInt()" and replaces it
with the , referenced by the $1-symbol.


Thanks for your help! i learned a lot

Answer

I think there might be a bit missing from the question but I'm going to make some assumptions and hope thats what you were going for.

I'm going to assume that NumberInt(8) is a string (if it's not you can operate on the object and pull out the first argument and set that as the value)

If we are looking to pull out what's in the parens we could use a basic Regexp (() around what we want as a second element):

v = "NumberInt(8)"
v.match(/NumberInt\((\d)\)/)
=> Array [ "NumberInt(8)", "8" ]

Should be able to parseInt() the second element and override the previous value.