【问题标题】:jQuery string replace using regex使用正则表达式替换 jQuery 字符串
【发布时间】:2015-05-21 13:16:18
【问题描述】:

我有一个这样的网址:

http://localhost/Customer/Edit/3

我需要检查 Customer/Edit/3 并将 3 替换为 jQuery 中的当前 ID (9)。

最终到达网址应类似于http://localhost/Customer/Edit/9

注意:替换必须仅适用于与客户相关的 URL。

我该怎么做?

【问题讨论】:

标签: jquery regex


【解决方案1】:

你不需要 jQuery:

var pattern = /(http:\/\/localhost\/Customer\/Edit\/)\d/;
var str = "http://localhost/Customer/Edit/3";
str = str.replace(pattern, '$1' + 9);
console.log(str); // returns http://localhost/Customer/Edit/9

以上内容应该足以让您创建适合您的解决方案。

如果您保证 URL 中只有一个数字,则可以改为执行以下操作:

var pattern = /\d/;
var str = "http://localhost/Customer/Edit/3";
str = str.replace(pattern, 9);
console.log(str);

【讨论】:

    猜你喜欢
    • 2015-02-27
    • 1970-01-01
    • 2018-07-13
    • 2015-11-30
    • 2021-11-29
    相关资源
    最近更新 更多