atrueresistance
atrueresistance

Reputation: 1368

javascript replace regex not global

//Get width and resize another element
$(document).ready(function() {
  function ResizeSearch(GridID, SearchID) {
    var eleWidth = $("#" + GridID).width();
    $("#" + SearchID).width(eleWidth);
  }
  $("#getp").click(function() {
    ResizeSearch("<%= gvValidStatus.ClientID %>", "ValidStatusSearch");
  });
});

This is a simple question for a lot of you stackoverflow people. I've got a little bit of code that looks like this:

var www = '?points=City%20Chairman%20Brutas&another=test';
var name = 'points';
var match = RegExp(RegExp('[?&]' + name + '=([^&]*)').exec(www)[1]);
match = decodeURIComponent(match); 
match = match.replace('\/','');
document.write(match);

The code is pretty straight forward, for jsfiddle purposes I made a var instead of using window.location.search. I understand that I NEED to make match.replace GLOBAL for this to work correctly. It currently outputs this:

City Chairman Brutas/

If I change match.replace to match.replace('\//g',''); it doesn't work.

Output of above: /City Chairman Brutas/

I know I'm close, if anybody could point me in the right direction that would be awesome.

Upvotes: 1

Views: 3826

Answers (2)

Willem Mulder
Willem Mulder

Reputation: 14014

match = match.replace(/\//g,'');

Should work. You need a regex-delimiter / at the start and the end of your regex. Your flags then come after the end-delimiter. Also, as pointed out below, you should not put quotes around your regex..!

Upvotes: 0

ThiefMaster
ThiefMaster

Reputation: 318728

You need to remove the quotes and use the /.../ regex literal syntax instead:

match.replace(/\//g,'');

Upvotes: 5

Related Questions