0

I'm using Javascript to grab a variable passed through the URL:

function get_url_parameter( param ){
  param = param.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var r1 = "[\\?&]"+param+"=([^&#]*)";
  var r2 = new RegExp( r1 );
  var r3 = r2.exec( window.location.href );
  if( r3 == null )
   return "";
  else
    return r3[1];
 }

Once I have the parameter required

var highlightsearch = get_url_parameter('search');

I want to be able to delete all of the string after the ">".

e.g

The result currently looks like this:

highlightsearch = "Approved%20XXXXX%20XXXXX>YYYY%20YYYYYYY%20YYYY%20-%20YYYY%20YYYY";

After my string manipulation I want it to hopefully look like

highlightsearch = "Approved%20XXXXX%20XXXXX";

Any help would be great.

Yi Jiang
  • 49,435
  • 16
  • 136
  • 136
  • 1
    Shameless plug, but your URL param function isn't very efficient, it doesn't work with encoded params or decode the result. See http://stackoverflow.com/questions/901115/get-querystring-values-with-jquery/2880929#2880929 for a more robust solution. – Andy E Nov 10 '10 at 10:01
  • Thanks Andy, learning a lot this evening! – darrenhamilton Nov 10 '10 at 10:18

2 Answers2

2

The following will get you everything before the ">":

var highlightsearch = get_url_parameter('search');

// highlightsearch = "1234>asdf"

highlightsearch = highlightsearch.slice(0, highlightsearch.indexOf(">"));

// highlightsearch = "1234"
stpe
  • 3,611
  • 3
  • 31
  • 38
1

Regular expression to match ">" and everything after it: >.*

highlightsearch = highlightsearch.replace(/>.*/, '')
Andy E
  • 338,112
  • 86
  • 474
  • 445
Mud
  • 28,277
  • 11
  • 59
  • 92