rockinaway Posted November 13, 2011 Share Posted November 13, 2011 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 More sharing options...
AyKay47 Posted November 14, 2011 Share Posted November 14, 2011 if the string is to be dynamic, then you will want to look into using a regex to match your search specifications. Link to comment https://forums.phpfreaks.com/topic/251082-jquery-slice-between-2-things/#findComment-1287981 Share on other sites More sharing options...
Adam Posted November 14, 2011 Share Posted November 14, 2011 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. Link to comment https://forums.phpfreaks.com/topic/251082-jquery-slice-between-2-things/#findComment-1287984 Share on other sites More sharing options...
rockinaway Posted November 14, 2011 Author Share Posted November 14, 2011 Yes I am using the current window URL. However, how do I exclude the stuff after # in the URL using window.location.search? (i'm reading up on it now but posting in case!) Link to comment https://forums.phpfreaks.com/topic/251082-jquery-slice-between-2-things/#findComment-1288027 Share on other sites More sharing options...
rockinaway Posted November 14, 2011 Author Share Posted November 14, 2011 Done it, thanks!. I used the following: var url = window.location.search.slice(window.location.search.indexOf('?') + 1).split('&'); Link to comment https://forums.phpfreaks.com/topic/251082-jquery-slice-between-2-things/#findComment-1288028 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.