これは、予約語が含まれている場合のコードの CSS の設定に関する私の質問へのフォローアップの質問です。
私がやろうとしていること: 一部のコードに引用符または二重引用符が含まれている場合、フォントの色を赤と太字に設定したいと考えています。元。 System.out.println( "Hello world" );
「Hello world」を赤に設定する必要があります。
何が問題なのか: 最善を尽くしたにもかかわらず、制御ステートメントを適切に機能させることができないようです (少なくとも、それが問題だと思います)。最初の二重引用符以降を赤に設定しますが、単語がanyword"またはanyword'に等しいときに停止するように指示すると、ブロック内の残りのコードが赤に設定されます。
HTML
<html>
<body>
<code id="java">
public static void main(String[] args)<br>
{
<pre> int i = 120; </pre><br>
<pre> // Displays a message in the console </pre>
<pre> // This is a test </pre>
<pre> System.out.println( "Hello Big World!" );</pre>
}
</code>
</body>
</html>
CSS
.quotes
{
font-weight: bold;
color: #E01B1B;
}
jQuery
$(document).ready(function() {
var code = $("#java").html(); // Get the code
var split = code.split(' '); // Split up each element
var chkQ = 0; // Check for quotes
var chkC = 0; // Check until end of comment line
// Set the CSS of reserved words, digits, strings, and comments
for (var j = 0; j < split.length; j++) {
// Check to see if chkQ is set to true
if (chkQ == 1) {
// If the element matches (anyword") or (anyword'), then set
// flag to false and continue checking the rest of the code.
// Else, continue setting the CSS to .quotes
if (split[j].match(/."/) || split[j].match(/.'/)) {
split[j] = '<span class="quotes">' + split[j] + '</span>';
chkQ = 0;
} else {
split[j] = '<span class="quotes">' + split[j] + '</span>';
}
}
...
} else if (chkQ == 0 && chkC == 0) {
...
// If the element matches a ("anyword) or ('anyword)...
} else if (split[j].match(/"./) || split[j].match(/'./)) {
split[j] = '<span class="quotes">' + split[j] + '</span>';
chkQ = 1;
} ...
}
}
// Join all the split up elements back together!
$("#java").html(split.join(' '));
});
質問: これは単なる正規表現、制御ブロック、またはまったく別の問題ですか?