【发布时间】:2020-12-15 03:37:32
【问题描述】:
有没有办法在 JavaScript 中从这个 URL 中删除 # 之后的任何内容
【问题讨论】:
标签: javascript
有没有办法在 JavaScript 中从这个 URL 中删除 # 之后的任何内容
【问题讨论】:
标签: javascript
你可以用#分割它,然后取第一个元素
const url = 'https://www.tripadvisor.co.uk/ShowUserReviews-g503793-d571919-r748731637-Premier_Inn_Waltham_Abbey_hotel-Waltham_Abbey_Essex_England.html#review748731637'
console.log(url.split('#')[0])
或者使用正则表达式匹配特定组
const url = 'https://www.tripadvisor.co.uk/ShowUserReviews-g503793-d571919-r748731637-Premier_Inn_Waltham_Abbey_hotel-Waltham_Abbey_Essex_England.html#review748731637'
const [_, res] = /(.*)#.*/.exec(url)
console.log(res)
【讨论】:
使用 String.split()
const url = "https://www.tripadvisor.co.uk/ShowUserReviews-g503793-d571919-r748731637-Premier_Inn_Waltham_Abbey_hotel-Waltham_Abbey_Essex_England.html#review748731637"
firstPartUrl = url.split('#')[0]
【讨论】: