0

I am trying to set a variable. The variable should be parts of a Search query in the URL. I'm assuming to use REGEX to achieve this. IE URL: http://tsqja.deznp.servertrust.com/SearchResults.asp?Search=true+ glass&x=0&y=0

I want to pull the words 'true' and 'glass' in the example above and throw them in a jQuery variable.

ie. var newKeys = $(search keys = some code);

I am no good at using REGEX but would this just be my variable?

/^http:\/\/(tsqja\.)?deznp\.servertrust\./i,/Search=([^&]+)/i

1 Answer 1

1

is this for a url alone or for the current url of the page the javascript is on? if the latter, it's as simple as

location.search.split("&")[0].split("=")[1]

otherwise you could do

var url = "http://tsqja.deznp.servertrust.com/SearchResults.asp?Search=true+glass&x=0&y=0";
url.match(/[?]Search=([^&]*)/)[1];

EDIT

So something like this might work (per your comments)

var url = "http://tsqja.deznp.servertrust.com/SearchResults.asp?Search=true+glass&x=0&y=0";
var theKeys = url.split("Search=")[1].split("&")[0].replace("+"," ");
Sign up to request clarification or add additional context in comments.

7 Comments

The site wide URL is (in this example) tsqja.deznp.servertrust and the code will be placed in a template so on every page. The URL I want is JUST for the url with the query Search= in it.
@ToddN but you only need true+glass when you're on a page with a url like http://tsqja.deznp.servertrust.com/SearchResults.asp?Search=true+glass&x=0&y=0 correct?
In the first example, could I use .split("Search=") so it would only pull from the URL with Search= in it?
I actually need something like this to be the end all result: keys: theKeys //true glass
@ToddN Yes you could, but you would probably need to do something like url.split("Search=")[1].split("&")[0] to just get the true+glass.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.