替换js中复杂字符串中的单词

时间:2013-10-03 06:34:33

标签: javascript

我有以下字符串:

$("#trip-to").after(
            '<div class="disambi">' + '<div class="disambiguation-indicator"></div>' + '<div class="disambiguation-inner-wrapper">' + ' <p><strong> &ldquo;'+query+ '&rdquo;.</strong></p>' + ' <p><strong>Did you mean to...</strong></p>' + '<ul>' + suggest + '</ul>' + (count >= 14 ? ' <p><a class="js-disambiguation-show-more" data-parent="to">Show more locations...</a></p>' : '') + '</div>' + '</div>');
        resultQuery = query;

我希望在某个条件下将上面的"to"替换为"from"。 我试过替换功能,但它没有给出正确的输出.... 可能是因为"'

这么多

我错过了其他任何事情?

1 个答案:

答案 0 :(得分:0)

为什么不将输出字符串创建为变量:

var outputStr = '<div class="disambi">' + '<div class="disambiguation-indicator"></div>' + '<div class="disambiguation-inner-wrapper">' + ' <p><strong> &ldquo;'+query+ '&rdquo;.</strong></p>' + ' <p><strong>Did you mean to...</strong></p>' + '<ul>' + suggest + '</ul>' + (count >= 14 ? ' <p><a class="js-disambiguation-show-more" data-parent="to">Show more locations...</a></p>' : '') + '</div>' + '</div>');

然后在变量上执行替换吗?

然后你可以简单地将修改后的字符串转储到你想要的位置:

$("#trip-to").after(outputStr);