0

I have a database outputting either:

  1. example-phrase
  2. example2-phrase or
  3. example-solution

And I need to replace these on the page load with:

  1. Example Phrase
  2. Example 2 Phrase
  3. Example & Solution

I already have jQuery loaded on the page so I can use a jQuery solution, but I know it is also possible with some basic Javascript...the problem is I do not know any of either and the examples I'm finding are not doing what I need. I also need to be able to add more replacements in the future.

UPDATE: I have this from another project, but it replaces everything before (and including ) the "=" in any h2 elements on the page. That first line: var re = /^[^=]+=\s*/, losses me with all the symbols, though:

<script>
    var re = /^[^=]+=\s*/,
        elements = document.getElementsByTagName('h2')

    function processText(el) {
        el.textContent = el.textContent.replace(re, '');
    }

    for(var i = 0, l = elements.length; i < l; i++) {
        processText(elements[i]);
    }
</script>
codeview
  • 209
  • 5
  • 15

2 Answers2

2

Have a look at this:

As you said, your string (1) is:

var ex1 = "example-phrase";
var res1 = ex1.replace("-", " ");
res1 = res1.toLowerCase().replace(/\b[a-z]/g, function(letter) {
    return letter.toUpperCase();
});

The above solution works for the first case. For the 3rd case, just add the following,

var ex3 = "example-solution";
var res3 = ex3.replace("-", " ");
res3 = res3.toLowerCase().replace(/\b[a-z]/g, function(letter) {
    return letter.toUpperCase();
});
res3 = res3.replace(" ", " & ");

I feel that checking every time you get a response isn't feasible.

Thanks to this answer

Community
  • 1
  • 1
1
var REPLACEMENTS = {
    'example-phrase': 'Example Phrase',
    'example2-phrase': 'Example 2 Phrase',
    'example-solution': 'Example & Solution'
}

function doReplacement(input) {
    for (var key in REPLACEMENTS) {
        input = input.replace(key, REPLACEMENTS[key]);
    }

    return input;
}

Might not be the most performant solution though. You might need to ask yourself: Do you need case insensitivity? Can a single input contain more than one of the strings that need to be replaced? Is the string itself exactly equal to the replacement key or does it contain it?

EDIT

(Updated) AS I see per your update, you probably want to do this for all h2 tags. Try this code:

$(function() {
    var REPLACEMENTS = {
        'example-phrase': 'Example Phrase',
        'example2-phrase': 'Example 2 Phrase',
        'example-solution': 'Example & Solution'
    }

    function doReplacement(input) {
        for (var key in REPLACEMENTS) {
            input = input.replace(key, REPLACEMENTS[key]);
        }

        return input;
    }

    $('h2').each(function() {
        var $this = $(this);
        $this.html(doReplacement($this.html()));
    });
});
Dennis
  • 14,210
  • 2
  • 34
  • 54
  • Exactly how you have it is how I would need it. The string getting replaced are always the same and what needs to replace them is always the same. What about this code is a performance hindrance? – codeview Nov 19 '13 at 08:23
  • It depends on how often you will run this and how many replacements there will be. If you this with, say, 20 headlines and 50 possible replacements you don't really need to care about its performance. It might become an issue if you run this over thousands of texts with hundreds of replacements. – Dennis Nov 19 '13 at 08:48
  • Ah, luckily just running on 1 page and probably only 10-20 replacements will happen. Do I need some other code to have this run automatically as soon as the page loads? – codeview Nov 19 '13 at 18:11
  • Yes, you need some more code - this function just does the replacement inside of a given string. will edit my answer, have another look :) – Dennis Nov 20 '13 at 07:02
  • Thanks again Chips_100, but I tried the code and it doesn't work (nothing happens): http://jsfiddle.net/codeview/E9cjt/ – codeview Nov 20 '13 at 18:04
  • @codeview my bad, there was a missing bracket in the last line `$this.html(doReplacement($this.html());`. Updated fiddle and my answer, have a look: http://jsfiddle.net/E9cjt/2/ – Dennis Nov 21 '13 at 06:35
  • 1
    Perfect! Thank you. Answered accepted, exactly what I needed. I did have to change `$` to `jQuery` for no conflict (I couldn't determine what other script was causing it) and changed `h2` to `.replacement` so I could apply this function with a css class. http://jsfiddle.net/codeview/GSZLR/ – codeview Nov 21 '13 at 18:14