【问题标题】:How can I use .replace to change a url structure from this+word to this-word?如何使用 .replace 将 url 结构从 this+word 更改为 this-word?
【发布时间】:2014-02-19 19:12:13
【问题描述】:

我想弄清楚如何将 Magento 中的查询字符串 url 从使用 + 符号替换为在单词之间使用破折号 (-)。我从 fishpig 购买了一个很棒的扩展:http://fishpig.co.uk/magento/extensions/seo-search-urls/

但我无法让它工作。 Ben(代码)一直在与我合作以尝试使功能正常运行,但我正在尝试主动一些。

在我看来,问题出在他代码中的 .replace 部分。

                var value = fld.getValue()
                .toLowerCase()
                    .replace(/^\s+|\s+$/g, '')
                        .replace('  ', ' ');

我所有的搜索 url 仍然有 /this+word/ 而不是 /this-word/ 但我看不到 .replace 中应该发生从 + 到 - 破折号的更改。

【问题讨论】:

  • .replace(/\+/g, '-') 您需要使用global 修饰符将所有+ 替换为-
  • 您将在代码中的哪个位置添加或更改它。我尝试了几种不同的方法,但到目前为止,结果仍然相同。

标签: javascript magento url


【解决方案1】:

你应该用反斜杠转义+

var string="some+thing+in+my+closet";
alert(string.replace(/\+/g,"-"));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多