最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to extract predefined pattern from a string? - Stack Overflow

programmeradmin1浏览0评论

Given the following string:

"<head></head><body>{"filesize":55312,"success":false,"msg":"incorrect-captcha-sol"}<div firebugversion="1.5.4" style="display: none;" id="_firebugConsole"></div></body>"

I need to extract the JSON string as follows:

{"filesize":55312,"success":false,"msg":"incorrect-captcha-sol"}

How should I do this?

Thank you

Given the following string:

"<head></head><body>{"filesize":55312,"success":false,"msg":"incorrect-captcha-sol"}<div firebugversion="1.5.4" style="display: none;" id="_firebugConsole"></div></body>"

I need to extract the JSON string as follows:

{"filesize":55312,"success":false,"msg":"incorrect-captcha-sol"}

How should I do this?

Thank you

Share Improve this question edited Aug 14, 2010 at 1:39 Delan Azabani 81.6k30 gold badges172 silver badges212 bronze badges asked Aug 14, 2010 at 1:38 q0987q0987 36.1k74 gold badges258 silver badges407 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3
 json = str.match(/(\{[^}]+\})/)[1];
json = str.match(/([^<]+)/)[1];

A regular expression like

({[^}]+})

would work.

Try it out using RegExr.

发布评论

评论列表(0)

  1. 暂无评论