Jump to content

jQuery slice between 2 things?


rockinaway

Recommended Posts

I am trying to slice a string but between two set points. I have a string with the value '&hello=now#".

 

I basically want to snip out the information between & and # so i can manipulate it. I can't just use substr as I have a varying length of the string. Any help as to how to do this?

Link to comment
https://forums.phpfreaks.com/topic/251082-jquery-slice-between-2-things/
Share on other sites

Is this the location of the current Window? If so you can use the window.location.search property to get exactly what you're after. If you just have a URL string in a variable, I would just use string functions for this:

 

var url = 'http://example.com/?foo=bar&baz=qux#hash';

if (url.indexOf('?') != -1) {
    var query = url.substr(url.indexOf('?') + 1);
    if (url.lastIndexOf('#') != -1) {
        query = query.replace(query.substr(query.lastIndexOf('#')), '');
    }
}

 

There's possibly a function or object that exists that I've overlooked that can do this already.

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.