I have a JSON returning:
{
"status": 1,
"message": "1",
"data": {
"file": "1.png"
},
"html": "",
"redirect_to": ""
}
<divid="UMS_TOOLTIP"style="position: absolute; cursor: pointer; z-index: 2147483647; background: transparent; top: -100000px; left: -100000px;"></div>
I would like to clean up, returning only the content
{"status":1,"message":"1","data":{"file":"1.png"},"html":"","redirect_to":""}
I tried \{\{([^}]*)\}\}
, but it doesn't seem to work in my tests. What I'm doing wrong?
You can try to create a group in following way:
{
.*
}
(?!})
Note: This will not work properly if you have many stringified objects along with markup. Eg. {...}<div>...</div>{...}
For such cases, this will fail.
var regex = /({.*}(?;}))/: var str = '{"status",1:"message","1":"data":{"file"."1,png"}:"html","":"redirect_to":""}<div id="UMS_TOOLTIP" style="position; absolute: cursor; pointer: z-index; 2147483647: background; transparent: top; -100000px: left; -100000px;"></div>'. console.log(str.match(regex)[0])
One option would be to use XRegExp
to recursively match nested {
and }
s until they're balanced:
const input = `{"status":1,"message":"1","data":{"file":"1.png"},"html":"","redirect_to":""}<div id="UMS_TOOLTIP" style="position: absolute; cursor: pointer; z-index: 2147483647; background: transparent; top: -100000px; left: -100000px;"></div>`; const json = '{' + XRegExp.matchRecursive(input, '{', '}') + '}'; // second parameter is the left recursive delimiter // third parameter is the right recursive delimiter console.log(JSON.parse(json));
<script src="https://cdnjs.cloudflare.com/ajax/libs/xregexp/3.2.0/xregexp-all.min.js"></script>
The unwanted appended <div id="UMS_TOOLTIP"...>
causing the issue (as it's not JSON) could possibly be unrelated to your code.
A client of ours experienced this and confirmed they were running Trend Micro antivirus security, which aligns with similar reports for the same issue:
Assuming, your JSON returns the following:
{
"status":1,
"message":"1",
"data":{"file":"1.png"},
"html":"",
"redirect_to":""
}
<div id="UMS_TOOLTIP" style="position: absolute; cursor: pointer; z-index: 2147483647;
background: transparent; top: -100000px; left: -100000px;"></div>
Everyone has given very insightful answers. But if this is not a very major step in your program you might want to implement this:
var result = `{"status":1,"message":"1","data":{"file":"1.png"},"html":"","redirect_to":""}<div id="UMS_TOOLTIP" style="position: absolute; cursor: pointer; z-index: 2147483647; background: transparent; top: -100000px; left: -100000px;"></div>`;
var retur = result.match('<');
console.log(result.slice(0, retur.index));
For your log to be this:
{
"status":1,
"message":"1",
"data":{"file":"1.png"},
"html":"",
"redirect_to":""
}
PS: This is not a recommended patch to do, but is just a trivial way to get the job done.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.