1
//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.

2 Answers 2

5

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

match.replace(/\//g,'');
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, I always thought the regex had to be in between ''
When using a string it's treated as plain text. The only way to use a string as a regex is passing it to the 'RegExp() constructor. And in this case you do not include any delimiters and pass the flags as a second argument.
0
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..!

2 Comments

When passing a string to replace() it's not a regex but a plain string. What you probably wanted was .replace(/\//g, '')
I must admit I've been using PHP lately... Edited and upvoted your answer ;-)

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.