I will have my url as this:
http://mysite.com/results.aspx?s=bcs&k="Hospital" OR "Office" OR "Facility"
I want to grab everything after 'k=', excluding 'k=' itself..
And this regex is partially working..but it grabs everything with k two times..
<script type="text/javascript">
document.write('<p>Showing Results for all' +
window.location.href.match(/[?&]k=([^&#]+)/) || [] + '</p>');
</script>
Javascript match function returns an array of matches in case of success or null if no match is found.
In your case first match is the whole string matched, second is backreference to ([^&#]+)
Probably better in this way:
match
is returning two elements. The first is a match of the entire regex. The 2nd element is the capturing group (what is within the()
). This is what you want, the 2nd element in the array.http://jsfiddle.net/7WcMc/
This should work:
http://jsfiddle.net/elclanrs/2aPCh/1/