javascript - 获取数据键值并包含在 .html 之前的 URL 中
问题描述
使用下面的代码从数据键中获取值并与 .html 连接。
初始网址将是 - http://www.test.com/en/location/london.html
基于收集的键值(例如:258888)需要附加在 URL 中,例如http://www.test.com/en/location/london.258888.html
但是,如果用户选择下一个选项值,现在它会获取最新的 URL 并包括新的密钥。在每个选择值上,它都包含到 URL。
喜欢 - http://www.test.com/en/location/london.258888.3899091.html
但是,它必须是唯一的。
http://www.test.com/en/location/london.3899091.html
$('a.dropdown-item').on('click', function(){
let getDataKey = $(this).attr('data-key');
let getWindowLocation = window.location.href;
let getLocationVal = getWindowLocation.replace(/\.html/, '.' + getDataKey + '.html');
window.location.replace(getLocationVal);
});
解决方案
[.0-9]
您还可以在正则表达式中包含可选的数字匹配器。
[0-9]
字符范围 0-9
*
零个或多个前面的字符。
代码示例:
$('a.dropdown-item').on('click', function(){
let getDataKey = $(this).attr('data-key');
let getWindowLocation = window.location.href;
let getLocationVal = getWindowLocation.replace(/[.0-9]*\.html/, '.' + getDataKey + '.html');
window.location.replace(getLocationVal);
});
演示:
let string1 = "http://www.test.com/en/location/london.html";
let string2 = "http://www.test.com/en/location/london.3899091.html";
console.log("String1 : " + string1.replace(/[.0-9]*\.html/, '.' + "454545.html"));
console.log("String2 : " + string2.replace(/[.0-9]*\.html/, '.' + "258888.html"));
推荐阅读
- oop - 为了不破坏 SRP,您是否在单独的类中定义应用程序的每个业务逻辑功能?
- excel - 如何在 VBA 中打开长路径文件?
- android - [FLUTTER]:PersistentBottomNavBar 包有问题
- ios - 放大手势和旋转手势 SwiftUI
- laravel-6 - Laravel 6. 语法错误,意外 ')',期待 '['
- azure - 无法从 AKS 中的 pod ping 任何外部 IP
- node.js - 如何从 node.js 中的 AWS Lambda 函数制作 mongodump?
- json - JQ,将CSV(父子格式)转换为JSON,另一个问题
- windows - CMake 在 Windows 上搜索 CUDA 编译器时使用的路径太长
- vue.js - vue-i18n 不使用 vuetify 组件字符串