我試圖匹配代碼塊來檢查是否是html或javascript代碼,我試過這樣做,但我有問題,而當這個html元素"<div></div>"
或php <? echo '';>
裏面的JavaScript代碼將它匹配爲html元素。識別代碼標籤之間的代碼是JavaScript還是HTML
請有人可以幫助我最好的方式來存檔這個?
<script>
$(document).ready(function(){
function AssignLang(theLanguage){
var regex = /(<([^>]+)>|<([^>]+)>)/ig;
//var regex = "<(\"[^\"]*\"|'[^']*'|[^'\">])*>";
if(theLanguage.match(regex)){
var lang = 'markup';
}else{
var lang = 'javascript';
}
return lang;
}
$('pre code').each(function() {
var the = $(this).html();
/*I tried here to match from 0 to 50 but is not going to help because if the javascript tag begin with <script> still show as html
var theLanguage = the.replace(/\s/g, '').substring(0,50);
*/
var theLanguage = the.replace(/\s/g, '');
var langType = AssignLang(theLanguage);
$(this).addClass("fullcoded language-"+langType);
alert(theLanguage+"-"+langType);
});
});
</script>
這裏我匹配碼前和代碼元素
<pre><code>
function check() {
var delvar = "<? $_POST["del"]; ?>";
var answer = confirm("Are you sure you want to delete the article?")
if (answer) {
window.location = "adm-site.php?del=" + delvar + "&delete=true";
}
}
</code></pre>
<pre><code>
<select name="del">
<option value="none" SELECTED></option>all articles echo'ed here by php
</select>
</code></pre>
這裏內部是https://jsfiddle.net/ppu9qw3n/
請確實需要幫助,我一直在做這個,謝謝4個小時 –
可能重複[RegEx match open tags not except XHTML self-contained tags](http://stackoverflow.com/questions/ 1732348/regex-match-open-tags-except-xhtml-self-contained-tags) –
@ Mr.George請讓我搜索解決方案,但是我真的不需要幫助 –