Javascript - RegExp in replace return matched string

Anonymous

I have this function that I use to replace the given keywords in a text with the very same keyword but in bold.

function parseKeywords(text){
    var replacedText = text,

        var keywords = getStoredData("currentCrisisKeywords").split(',');

        $(keywords).each(function(i, kw){
            var kWord = kw.replace(/^\s+|\s+$/g, "");
            replacedText = replacedText.replace(new RegExp(kWord, 'i'), '<b>'+kWord+'</b>');
        });
        return replacedText;
}

The problem is that kWord may be "this is a test" while the replaced string was "This is a Test". I need to mantain the original format, while now by replacing it with the keyword, I cannot keep the original format.

I tried this way with no luck:

replacedText = replacedText.replace(new RegExp(kWord, 'i'), "<b>$1</b>");

Do you know a way to return the matched string by mantaining the original format even though the regExp search is case insensitive?

Thanks

Guffa

Put parentheses around the keyword in the pattern so that it is caught, then you can use $1 in the replacement string to use the matched string instead of the keyword:

var kWord = "(" + kw.replace(/^\s+|\s+$/g, "") + ")";
replacedText = replacedText.replace(new RegExp(kWord, 'i'), '<b>$1</b>');

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

replace javascript regexp matched group with a dollar sign

From Dev

replace javascript regexp matched group with a dollar sign

From Dev

Oracle, replace string for REGEXP_REPLACE based on the matched expression

From Dev

Javascript string replace function on matched group

From Dev

Javascript string replace using regExp and replacer function

From Dev

regexp to replace string after third '-' in javascript

From Dev

Javascript string replace using regExp and replacer function

From Dev

Doubts in JavaScript RegExp and String.replace() method

From Dev

Javascript replace regexp string between square brackets

From Dev

replace with regexp return undefined

From Dev

Replace String in python with matched pattern

From Dev

Algorithm to replace matched patterns in string

From Dev

Replace String in python with matched pattern

From Dev

How to replace matched string php

From Dev

Regexp to replace repeating string

From Dev

Regexp to replace string

From Dev

Find all matched selectors with RegExp in JavaScript

From Dev

Find all matched selectors with RegExp in JavaScript

From Dev

javascript replace with regexp?

From Dev

Regexp javascript and replace

From Dev

javascript regexp - replace all floating point numbers in a string with rounded numbers

From Dev

Replace part of a matched regexp with sed or any other tool

From Dev

Replace part of a matched regexp with sed or any other tool

From Dev

finding matched string with grep does not return the matched value

From Dev

How to replace matched character String in java?

From Dev

replace the matched string in a file with ref to multi line

From Dev

Replace inside matched string with Notepad++ and regex

From Dev

How to replace matched character String in java?

From Dev

replace two separate words in a matched string sed

Related Related

  1. 1

    replace javascript regexp matched group with a dollar sign

  2. 2

    replace javascript regexp matched group with a dollar sign

  3. 3

    Oracle, replace string for REGEXP_REPLACE based on the matched expression

  4. 4

    Javascript string replace function on matched group

  5. 5

    Javascript string replace using regExp and replacer function

  6. 6

    regexp to replace string after third '-' in javascript

  7. 7

    Javascript string replace using regExp and replacer function

  8. 8

    Doubts in JavaScript RegExp and String.replace() method

  9. 9

    Javascript replace regexp string between square brackets

  10. 10

    replace with regexp return undefined

  11. 11

    Replace String in python with matched pattern

  12. 12

    Algorithm to replace matched patterns in string

  13. 13

    Replace String in python with matched pattern

  14. 14

    How to replace matched string php

  15. 15

    Regexp to replace repeating string

  16. 16

    Regexp to replace string

  17. 17

    Find all matched selectors with RegExp in JavaScript

  18. 18

    Find all matched selectors with RegExp in JavaScript

  19. 19

    javascript replace with regexp?

  20. 20

    Regexp javascript and replace

  21. 21

    javascript regexp - replace all floating point numbers in a string with rounded numbers

  22. 22

    Replace part of a matched regexp with sed or any other tool

  23. 23

    Replace part of a matched regexp with sed or any other tool

  24. 24

    finding matched string with grep does not return the matched value

  25. 25

    How to replace matched character String in java?

  26. 26

    replace the matched string in a file with ref to multi line

  27. 27

    Replace inside matched string with Notepad++ and regex

  28. 28

    How to replace matched character String in java?

  29. 29

    replace two separate words in a matched string sed

HotTag

Archive