Если я правильно понял, вы можете достичь того, что вы хотите, используя $.inArray
и обернув зарезервированное слово тегом span
.См. Мою DEMO
Редактировать: Ниже приведена документация jQuery $ .inArray.
$.inArray( value, array [, fromIndex] )
-
value Значение для поиска.
arrayAn массив для поиска.
fromIndex Индекс массива вс чего начать поиск.По умолчанию 0, что будет искать весь массив.
.. читать дальше ..
CSS
.code {
font-weight: bold;
color: #2400D9;
}
JS
$(document).ready(function() {
// Get the text inside the code tags
var code = $("#java").html();
// Split up each word
var split = code.split(' ');
// Array of reserved words
var array = ["abstract", "assert", "boolean", "break", "byte", "case", "catch", "char", "class", "const", "continue", "default", "do", "double", "else", "else if", "enum", "extends", "final", "finally", "float", "for", "goto", "if", "import", "implements", "instanceof", "int", "interface", "long", "native", "new", "null", "package", "private", "protected", "public", "return", "short", "static", "strictfp", "super", "switch", "synchronized", "this", "throw", "throws", "transient", "void", "volatile", "while"];
for (var j = 0; j < split.length; j++) {
if ($.inArray(split[j], array) > 0) {
split[j] = '<span class="code">' + split[j] + '</span>';
}
}
$("#java").html(split.join(' '));
});