迹忆客 专注技术分享

当前位置:主页 > 学无止境 > WEB前端 > JavaScript >

使用 JavaScript 从 URL 中删除查询字符串

作者:迹忆客 最近更新:2022/12/03 浏览次数:

要从 url 中删除查询字符串,请使用 split() 方法在问号上拆分字符串并访问索引 0 处的数组元素,例如 url.split('?')[0]split 方法将返回一个包含 2 个子字符串的数组,其中第一个元素是查询字符串之前的 url。

const url = 'https://example.com/books?page=10#hash';

// ✅ Remove querystring
const result1 = url.split('?')[0];
console.log(result1); // 👉️ "https://example.com/books"

// ✅ Remove querystring
const result2 = url.slice(0, url.indexOf('?'));
console.log(result2); // 👉️ "https://example.com/books"

// ✅ Preserve hash
const index = url.indexOf('?');
const result3 = url.slice(0, url.indexOf('?')) +
                url.slice(url.indexOf('#'));
console.log(result3); // 👉️ "https://example.com/books#hash"

我们传递给 String.split 方法的唯一参数是我们要拆分字符串的分隔符。

const url = 'https://example.com/books?page=10#hash';

// 👇️ ['https://example.com/books', 'page=10#hash']
console.log(url.split('?'));

该方法返回一个包含 2 个字符串的数组 - 问号之前的一个和问号之后的一个。

此解决方案还处理 url 不包含查询字符串的情况。

const url = 'https://example.com/books';

// 👇️ ['https://example.com/books']
console.log(url.split('?'));

如果我们传递一个不包含在字符串中的子字符串,split() 方法将返回一个包含整个字符串的数组。

另一种方法是使用 String.indexOf 方法获取问号的索引。

要从 url 中删除查询字符串,请在 url 上调用 slice() 方法并获取从索引 0 到字符串中问号索引的字符。 slice 方法将返回一个不包含查询字符串的新字符串。

const url = 'https://example.com/books?page=10#hash';

const result2 = url.slice(0, url.indexOf('?'));
console.log(result2); // 👉️ "https://example.com/books"

我们传递给 String.slice 方法的 2 个参数是:

  • start 索引 - 要包含在字符串中的第一个字符的索引
  • stop 索引 - 上升到但不包括该索引
  • indexOf 方法返回子串在字符串中第一次出现的索引,如果子串不在字符串中则返回 -1。

如果选择这种方法,这是我们需要处理的潜在边缘情况。

const url = 'https://example.com/books';

let result2 = url;

if (url.includes('?')) {
  result2 = url.slice(0, url.indexOf('?'));
}

console.log(result2); // 👉️ "https://example.com/books"

我们的 if 语句处理 url 不包含查询字符串的情况。

如果我们需要删除查询字符串,但又想保留散列,请将对 slice 方法的两次调用连接起来。

const url = 'https://example.com/books?page=10#hash';

// // ✅ Preserve hash
const result3 = url.slice(0, url.indexOf('?')) +
                url.slice(url.indexOf('#'));

console.log(result3); // 👉️ "https://example.com/books#hash"

slice 方法的第一次调用获取查询字符串之前的 url 部分。

第二次调用包含从散列开始的字符串。

如果将单个参数传递给 slice 方法,它会将字符包含到字符串的末尾。

我们可能需要处理 url 不包含查询字符串或哈希的情况。

const url = 'https://example.com/books?page=10#hash';

let result3 = url;

if (url.includes('?') && url.includes('#')) {
  result3 = url.slice(0, url.indexOf('?')) +
            url.slice(url.indexOf('#'));
}

console.log(result3);

在此示例中,如果 URL 包含查询字符串和哈希,我们仅重新分配 result3 变量,否则我们按原样返回 url。

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

Do you understand JavaScript closures?

发布时间:2025/02/21 浏览次数:108 分类:JavaScript

The function of a closure can be inferred from its name, suggesting that it is related to the concept of scope. A closure itself is a core concept in JavaScript, and being a core concept, it is naturally also a difficult one.

Do you know about the hidden traps in variables in JavaScript?

发布时间:2025/02/21 浏览次数:178 分类:JavaScript

Whether you're just starting to learn JavaScript or have been using it for a long time, I believe you'll encounter some traps related to JavaScript variable scope. The goal is to identify these traps before you fall into them, in order to av

How much do you know about the Prototype Chain?

发布时间:2025/02/21 浏览次数:150 分类:JavaScript

The prototype chain can be considered one of the core features of JavaScript, and certainly one of its more challenging aspects. If you've learned other object-oriented programming languages, you may find it somewhat confusing when you start

JavaScript POST

发布时间:2024/03/23 浏览次数:96 分类:JavaScript

本教程讲解如何在不使用 JavaScript 表单的情况下发送 POST 数据。

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便