开发者

Javascript need regex to add to the end of a string

开发者 https://www.devze.com 2023-02-17 09:36 出处:网络
I\'m trying to add a variable to the end of my href using regex and jquery. This is what i have so far:

I'm trying to add a variable to the end of my href using regex and jquery. This is what i have so far:

$('#survey-click1').click(function(event) {
      playerPause();
      $("a[href^=http://d.surveysonline.com/]")
          .each(function()
      {
        this.href = [regex goes here];
      });
      return false;
});

I'm simply t开发者_开发百科rying to add a variable at the end of the url i clicked on to attach whether or not the video i just watched had a preroll or not. This could be done easily by attaching this to the end of my url code [&preroll="+Kdp3State.preroll+"]. How do i detect the end of the href string to attach this to to it?

Thanks


Assuming that this is actually what you want to append to your URL, this should work:

$("a[href^=http://d.surveysonline.com/]").each(function() {
    $(this).attr('href', $(this).attr('href', '&preroll='+Kdp3State.preroll);
});

Apart from the fact that you use a CSS selector that is somewhat similiar to a regex, this question seems be unrelated to the subject.


There's no need for a regexp, just do:

this.href += 'string_to_be_appended';

e.g.

this.href += '&preroll=' + encodeURIComponent(Kdp3State.preroll);

Note the use of encodeURIComponent to ensure that any special characters (+, %, etc) in the resulting URI are correctly encoded.


this.href += "&preroll=" + Kdp3State.preroll

or

this.href = this.href.replace(/&preroll=[^&]+/g, "") + "&preroll=" + Kdp3State.preroll;

?

0

精彩评论

暂无评论...
验证码 换一张
取 消