从标签中取值

来源:互联网 发布:桌面视频录制软件 编辑:程序博客网 时间:2024/05/01 09:29

简述:

在服务器返回数据的时候,发现虽然response的是JSON的String但是服务器那里自己会加上<pre ...>Json Str<\pre>标签


这里写个小程序,正则匹配之后取出这个标签,返回里面的Json String


代码:

<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><script type = "text/javascript">function getJSONStr(){var rawStr = document.getElementById('inputText').value;var reg = /<pre.+?>(.+)<\/pre>/g;var result = rawStr.match(reg);document.getElementById('result').innerHTML = RegExp.$1;alert(RegExp.$1);}</script></head><body><B>解析带标签的字符串</B><br><br><input type= "text" id = 'inputText' value = '' /><input type = 'button' value = "exec" onclick = 'getJSONStr()'/><br><br><B>result:<B> <b id = 'result'></b></body></html>



输入:

<pre style="word-wrap: break-word; white-space: pre-wrap;">{"unit":{}}</pre>



输出:



原创粉丝点击