我从ajax GET请求获得以下响应(使用$ .Ajax )
<div class="clienthoverpage_content">
<div class="school_info" id="lesson_51123">
<div class="_desc_content" id="lesson_content">
<div class="_desc_icon">
</div>
<div class="_desc_description">
<h1 class="hover_name" id="lesson_name"></h1>
<div class="fraud_warning" id="lesson_fraud_warnings"></div>
<div class="_desc_class_info" id="lesson_class_info">
<div id="lesson_class_name" class="ellipsis"></div>
<div id="lesson_type" class=""></div>
</div>
<div class="_desc_descriptors" id="lesson_descriptors">
</div>
</div>
</div>
</div>
</div>
<script type="text/javascript">
fnInitDisplay = function() {
UserYou.LoadContexts( g_rgAppContextData );
BuildHover( 'lesson_51123', {"id":"123441","class":"51112","student":"506854340"} );
$('lesson_51123').show();
}
</script>
我感兴趣的一行是
BuildHover(&#39; lesson_51123&#39;, 的 {&#34; ID&#34;:&#34; 123441&#34;&#34;类&#34;:&#34; 51112&#34;&#34;学生&#34;:& #34; 506854340&#34;} );
我想采用 id ,类,学生的值。 我假设这样做的最佳方式是使用正则表达式,但我找不到合适的正则表达式。
答案 0 :(得分:1)
我在Regex上的表现并不是很好,所以我会举例说明我只使用indexOf和substring方法。 JS-FIDDLE
var str = 'UserYou.LoadContexts( g_rgAppContextData );'+
'BuildHover( "lesson_51123", {"id":"123441","class":"51112","student":"506854340"} );'+
'$("lesson_51123").show();';
var _id = '"id":"';
var _class = '","class":"';
var _student = '","student":"';
var classIndex = str.indexOf(_class);
var idIndex = str.indexOf(_id);
var studentIndex = str.indexOf(_student);
var endStr = str.indexOf('"}');
var resultID = str.substr(idIndex + _id.length, classIndex - (idIndex + _id.length) );
var resultClass = str.substr(classIndex + _class.length, studentIndex - (classIndex + _class.length));
var resultStudent = str.substr(studentIndex + _student.length, str.length-2 - (studentIndex + _student.length));
答案 1 :(得分:0)
var a = str.match(/BuildHover\(\s*'.*?',\s*(.*?)\s*\)/);
// Note a[1] will be a string use JSON.parse(a[1]) to get an object
console.log(a[1]); // {"id":"123441","class":"51112","student":"506854340"}
故障:
/ # start regex
BuildHover\( # "BuildHover("
\s* # 0 or more spaces
' # single quote ("'")
.*? # everything non gready
', # Match "',"
\s* # 0 or more spacres
( # start capture group
.*? # everything non gready
) # end group
\s* # 0 or more spaces
\) # ")"
/ # end regex
答案 2 :(得分:0)
n=input("Enter a list:")
n.sort()
l=len(n)
n.remove(n[l-1])
l=len(n)
print n[l-1]
您可以在结果中找到String,然后使用JSON.parse获取对象。尽管做那些事情并不是很干净。