-1

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.

2
  • 1
    Shameless plug, but your URL param function isn't very efficient, it doesn't work with encoded params or decode the result. See stackoverflow.com/questions/901115/… for a more robust solution. Commented Nov 10, 2010 at 10:01
  • Thanks Andy, learning a lot this evening! Commented Nov 10, 2010 at 10:18

2 Answers 2

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"
Sign up to request clarification or add additional context in comments.

Comments

1

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

highlightsearch = highlightsearch.replace(/>.*/, '')

1 Comment

Thanks folks. Working on it now.

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.