javascript - 设置用双引号括起来的单词的 CSS

标签 javascript jquery html css string

这是我关于 Setting the CSS of code if it contains a reserved word 的问题的后续问题.

我想做什么:如果某些代码有引号或双引号,我想将字体颜色设置为红色和粗体。前任。 System.out.println( "Hello world"); 应将“Hello world”设置为红色。

出了什么问题:尽管我尽了最大的努力,但我似乎无法让我的控制语句正常工作(至少我认为这是问题所在)。它将第一个双引号和后面的双引号设置为红色,但是当我告诉它在一个单词等于 anyword"anyword' 时停止时,它将其余代码设置为 block 变红。

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(' '));
});

问题:这只是我的正则表达式、控制 block 或完全不同的东西的问题吗?

最佳答案

既然可以执行简单的全局正则表达式查找和替换,为什么还要拆分字符串:

<script type="text/javascript">
$(document).ready(function(){
//cache the element
   el = $('#java');
//get the HTML contained within the cached element
   code = el.html();
//return the code having executed the replace method, regex explained:
/*    
([^\w]{1}) -> look for a single character that is not an alpha character
(["']) -> then look for either a single quote or double quote
(.*?) -> then look any character, but don't be greedy
(\2) -> then look for what was found in the second group - " or '
([^\w]{1}) -> and finally look for a single character that is not an alpha character
*/
    code = code.replace(/([^\w]{1})(["'])(.*?)(\2)([^\w]{1})/gm,
//execute an anonymous callback, passing in the result for every match found
    function(match, $1, $2, $3, $4, $5, offset, original) {
//construct the replacement
        str =  $1 + '<span class="quotes">' + $2 + $3 + $4 + '</span>' + $5; 
//return the replacement
        return str; 
    });
//replace the existing HTML within the cached element
   el.html(code);
});
</script>

编辑:刚刚更新它以适应嵌套引号。

关于javascript - 设置用双引号括起来的单词的 CSS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10182618/

相关文章:

javascript - 当输入更改元素值时隐藏元素的通用函数

javascript - 使用 Alpine JS 切换元素与类?

javascript - 如何一键重置所有按钮的值

javascript - 如何在 FireFox 浏览器上隐藏 Powertip 工具提示

jquery - 当 ul ul 活跃时 ul parent 移动

html - 创建可点击元素列表

javascript - jQuery 最小部分高度等于窗口高度

javascript - 使用 Ruby 的 arr.inject( :+) or JavaScript's arr. reduce((a,b) => { return a + b }) 对数字数组求和是不正确的吗?

javascript - 3 分钟后显示购买按钮,但在第二次或更多页面浏览后立即显示(首选 Javascript)

jQuery 延迟问题