0

I have the following text in a textarea:

<textarea class="content">
    [shortcode1 param1=test param2=john]
    [shortcode2 param1=test param2=john]
    [shortcode1 param1=john param2=exchange]
</textarea>

How I want to change the parameter names, but only if its shortcode1

So this should be the result:

<textarea class="content">
    [shortcode1 newparam1=test newparam2=john]
    [shortcode2 param1=test param2=john]
    [shortcode1 newparam1=john newparam2=exchange]
</textarea>

I currently use jQuery the following way:

$(".content").text($(".content").text().replace("param1", "newparam1"));
$(".content").text($(".content").text().replace("param2", "newparam2"));

But as you can see, it takes not only the params of shortcode1, but all.

Can somebody help me? And is there a way to optimize the jquery part, because I have 10 of this replacements and may I can combine them?

Zoker
  • 2,020
  • 5
  • 32
  • 53
  • The shortcode is executed in PHP, replacing it with javascript doesn't really make any difference, nor is it possible. – adeneo Mar 03 '15 at 12:29
  • I not want to replace the shortcode with javascript, I want to replace a shortcode with an other shortcode, this done by javascript. See the code above... – Zoker Mar 03 '15 at 13:11

1 Answers1

1

Search (with indexOf) in each line (with split):


var text = $(".content").text().split("]");

for (var i=0, l=text.length; i<l; i++) {
    if (text[i].indexOf("shortcode1") >= 0) {
        text[i] = text[i].replace("param1", "newparam1").replace("param2", "newparam2");
    }
}

$(".content").text( text.join("]") );