JavaScript – Issue using multiple keywords with RegExp

Shrey

I'm trying to make a syntax highlighting system for Java code using JavaScript. So far, I've figured out how to color individual words, but when I try to color multiple words, it replaces the new keyword with the original one. Here is a picture example: enter image description here

The "main" in the beginning is supposed to say public. How do I do JavaScript RegExp for multiple keywords and still retain my text? Code: http://jsfiddle.net/vtnd02e7/

<h3>Original Text</h3>
<textarea id="origtext">
</textarea>
<button onclick="colorize()">Colorize</button>

<h3>Colorized Text</h3>
<pre id="newtext"></pre>

<script>
function colorize() {
    var str = document.getElementById("origtext").value;
    var newstr = str.replace(/(main)|(public)/gi, "<span style='color:red;'>main</span>");
    document.getElementById("newtext").innerHTML = newstr;
}
</script>
Paul S.

Either use the same capture group and have the captured value go into the string, e.g.

var newstr = str.replace(
    /(main|public)/gi,
    "<span style='color:red;'>$1</span>"
);

Or use a function as the second arg of .replace which has some choice logic, e.g.

var newstr = str.replace(
    /(main)|(public)/gi,
    function ($0, $1, $2) {
        return "<span style='color:red;'>" + ($1 || $2) + "</span>";
    }
);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Regexp for multiple keywords matching

From Dev

REGEXP match either keywords in the string using MySQL

From Dev

JavaScript RegExp with multiple modifiers

From Dev

Javascript: RegExp Turkish Character Issue

From Dev

Javascript RegExp - test Method issue

From Dev

Javascript - Regexp - Global search Issue

From Dev

Weird javascript regexp replace issue

From Dev

Recognizing multiple keywords using PocketSphinx

From Dev

Split using multiple keywords as demiliters

From Dev

Javascript RegExp match & Multiple backreferences

From Dev

Split using multiple keywords using regex

From Dev

Using multiple keywords in pyplot.axis in matplotlib

From Dev

Chrome extension using multiple omnibox keywords

From Dev

Using multiple keywords in xattr via _kMDItemUserTags or kMDItemOMUserTags

From Dev

search through a database using multiple keywords in php

From Dev

Using excel search function while there are multiple keywords

From Dev

AngularJS search multiple keywords using one field

From Dev

Matching multiple keywords using contains() in jQuery

From Dev

using variable in jquery, javascript regexp

From Dev

Validate url using regexp in javascript

From Dev

extract data using regexp in javascript

From Dev

Replace keywords from text using javascript

From Dev

How to write big regexp in multiple lines javascript?

From Dev

Using multiple regexp parameter for Angular UI Router

From Dev

Replace multiple capture groups using regexp with java

From Dev

Using regular expression for fetching multiple keywords using python 3.4

From Dev

how to split a string which has multiple repeated keywords in it to an array in javascript?

From Dev

Javascript string replace using regExp and replacer function

From Dev

JavaScript regex with variable pattern using RegExp constructor

Related Related

  1. 1

    Regexp for multiple keywords matching

  2. 2

    REGEXP match either keywords in the string using MySQL

  3. 3

    JavaScript RegExp with multiple modifiers

  4. 4

    Javascript: RegExp Turkish Character Issue

  5. 5

    Javascript RegExp - test Method issue

  6. 6

    Javascript - Regexp - Global search Issue

  7. 7

    Weird javascript regexp replace issue

  8. 8

    Recognizing multiple keywords using PocketSphinx

  9. 9

    Split using multiple keywords as demiliters

  10. 10

    Javascript RegExp match & Multiple backreferences

  11. 11

    Split using multiple keywords using regex

  12. 12

    Using multiple keywords in pyplot.axis in matplotlib

  13. 13

    Chrome extension using multiple omnibox keywords

  14. 14

    Using multiple keywords in xattr via _kMDItemUserTags or kMDItemOMUserTags

  15. 15

    search through a database using multiple keywords in php

  16. 16

    Using excel search function while there are multiple keywords

  17. 17

    AngularJS search multiple keywords using one field

  18. 18

    Matching multiple keywords using contains() in jQuery

  19. 19

    using variable in jquery, javascript regexp

  20. 20

    Validate url using regexp in javascript

  21. 21

    extract data using regexp in javascript

  22. 22

    Replace keywords from text using javascript

  23. 23

    How to write big regexp in multiple lines javascript?

  24. 24

    Using multiple regexp parameter for Angular UI Router

  25. 25

    Replace multiple capture groups using regexp with java

  26. 26

    Using regular expression for fetching multiple keywords using python 3.4

  27. 27

    how to split a string which has multiple repeated keywords in it to an array in javascript?

  28. 28

    Javascript string replace using regExp and replacer function

  29. 29

    JavaScript regex with variable pattern using RegExp constructor

HotTag

Archive