Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript Character replace using jquery

  • This topic is empty.
Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #158360
    bastunqk
    Participant

    Hi.
    I’m trying to replace character in url using jquery.
    Here’s what i’ve tried so far.
    var s = $(‘.breadWhole a:nth-of-type(3)’).attr( ‘href’ );
    s.replace(‘//’, ‘/’);
    alert(s);
    So i take the link it is something like http://example.com//test
    I need to remove second // from the url and make it with one /.
    Please help.
    Regards.

    #158362
    CrocoDillon
    Participant

    It’s late and I’m sleepy so not sure if this is the best solution, but this RegEx should work /([^:]\/)\//g, use it like:

    s = s.replace(/([^:]\/)\//g, '$1');
    

    Basically it replaces every // with / as long as it’s not preceded by a colon (like in http://).

    Not jQuery, but I don’t think jQuery got any helper functions for this anyway. Curious about use cases though.

    #158384
    bastunqk
    Participant

    Thank you. It worked like a charm.

Viewing 3 posts - 1 through 3 (of 3 total)
  • The forum ‘JavaScript’ is closed to new topics and replies.