Click here to Skip to main content
15,884,537 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have been working on this to replace every code that is shared on post in my website but is not working for me.
I need everything that appear inside `
<code></code>
` to replay all the tags and give it a color using css. please if there is any simple way to do this can someone just help me with that? i will appreciate sample code in jsfiled


And here is how codes display in my website

HTML
<pre id="Mpreditor" class="code code-codes language-javascript">
    <code ng-transclude="ng-transclude" class="language-javascript">    
    <span>Hello</span>
    You have to include the `%` signs in the `$params`, not in the query:
    
    $query = "SELECT * FROM tbl WHERE address LIKE ? OR address LIKE ?";
    $params = array("%$var1%", "%$var2%");
    $stmt = $handle->prepare($query);
    $stmt->execute($params);
    </code>
</pre>


What I have tried:

here is sample of code that i have been trying to use


JavaScript
function Rep(text){
    text = text.replace(/ /g," ");

    text = text.replace(/<([^\>]+?)>/g, function(match, contents){
        return "<span class="TagColor"><"+contents+"></span>";
    }
    );

    text = text.replace(/\"([^\"]+?)\"/g, function(match, contents){
        return '<span class="StringColor">"'+contents+'"</span>';
      }
    );
    text = text.replace(/\(([^\)]+?)\)/g, function(match, contents){
        return '<span class="OptionsColor">('+contents+')</span>';
      }
    );
    text = text.replace(/\n/g, function(match, contents){
        return '<br />';
    }
    );
    text = text.replace(/\{/g, function(match, contents){
        return '<span class="BracketsColor">{</span>';
    }
    );
    text = text.replace(/\}/g, function(match, contents){
        return '<span class="BracketsColor">}</span>';
    }
    );
    text = text.replace(/\/\*([^\*]+?)\*\//g, function(match, contents){
        return '<span class="CommentColor">/*"+contents+"*/</span>';
    }
    );
      text = text.replace(/\[\](.*?)\[\/\]/g, function(match, contents){
        return '<span class="BBColor">'+contents+'</span>';
    }
    );
    return text;

  }

  //HTML replace
  //==================================================
  $('code').each(function(){
      var text=$(this).html().replace(/       var text=text.replace(/>/g,'>');
      $(this).html(text);
  });

  //google-code-prettify
  //==================================================
  jQuery(window).load(function(){
      prettyPrint();
  });

  //Still on Text format

  var app = angular.module('codelab',[],['$interpolateProvider',
      function($interpolateProvider) {
          $interpolateProvider.startSymbol('[[');
          $interpolateProvider.endSymbol(']]');
          }]);

  app.directive('ngPrism',['$interpolate', function ($interpolate) {
          "use strict";
          return {
            restrict: 'E',
            template: '<pre id="Mpreditor" class="code code-codes language-javascript"><code ng-transclude="ng-transclude" class="language-javascript"> </code></pre>',
            replace:true,
            transclude:true
          };
      }]);
Posted
Updated 7-Jun-16 12:35pm
v2
Comments
Beginner Luck 8-Jun-16 2:16am    
hi, you are try to convert to below to code by jquery
You have to include the `%` signs in the `$params`, not in the query:

$query = "SELECT * FROM tbl WHERE address LIKE ? OR address LIKE ?";
$params = array("%$var1%", "%$var2%");
$stmt = $handle->prepare($query);
$stmt->execute($params); to a code???

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900