【问题标题】:How to remove characters in Javascript [duplicate]如何删除Javascript中的字符[重复]
【发布时间】:2020-12-15 03:37:32
【问题描述】:

【问题讨论】:

    标签: javascript


    【解决方案1】:

    你可以用#分割它,然后取第一个元素

    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)

    【讨论】:

      【解决方案2】:

      使用 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]
      

      【讨论】:

        猜你喜欢
        • 2018-06-13
        • 2014-04-08
        • 2015-06-07
        • 2019-09-02
        • 1970-01-01
        • 2019-06-02
        • 1970-01-01
        • 1970-01-01
        • 2013-02-13
        相关资源
        最近更新 更多