1

What I'm trying to do is replace some a bit of "placeholder" text which is inside a URL string , with another string that is stored inside a javascript variable. This variable is dynamically generated based on a user's choices, so I can't just hardcode the variable.

Here is my code thus far:

var result_photo = "http://quizsocial.com/yahoo/this.jpg";
var sRegExphoto = new RegExp(result_photo);

$('a.facer').each(function() {
this.attributes.onclick.nodeValue=this.attributes.onclick.nodeValue.replace('result_photo_placeholder', sRegExphoto);
});

You can also see my jsfiddle at: http://jsfiddle.net/Ywa7j/1/

This fiddle seems very close to me, but where I get stuck is that my javascript Regex bit that gets replaced into my string continues to have it's regex delimiters set around it. If I could get rid of those delimiters on my Regex object, I think I would be doing well. However, I simply cannot find a way to get rid of those delimiters.

Please help me figure this one out! Thanks a lot. If you have suggestions that will take me away from the way I'm attempting to do this now, I'm all ears, as well.

3
  • I don't really see why you need a regex at all here. Why not just .replace('result_photo_placeholder', result_photo); Commented Mar 11, 2014 at 2:28
  • I had read online that I needed to pass a regex object to the .replace function - as in, it simply won't take a simple variable. Maybe I am wrong though... Commented Mar 11, 2014 at 2:38
  • That's not correct: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… You've gone about this in a really backwards fashion, perhaps just explain what you want to do, as opposed to a solution you've already decided upon. Commented Mar 11, 2014 at 3:56

2 Answers 2

2

My solution:

http://jsfiddle.net/Ywa7j/2/

Description:

replace: this.attributes.onclick.nodeValue=this.attributes.onclick.nodeValue.replace('result_photo_placeholder', sRegExphoto);

With: $(this).attr('onclick',$(this).attr('onclick').replace('result_photo_placeholder',sRegExphoto));

you must use $(this) instead of "this" and use the attr method to set a new value. In your case, set it as the same value replacing "result_photo_placeholder".

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @JMZavala! I had to make a little change to your code - you are using "sRegExphoto" still, whereas I just want to use the base result_photo variable - but your basic function was definitely the key to opening this up for me and I really appreciate it!
0

Rather that wrap your string in a RegExp object, pass the string to the replace function.

When you pass the regular expression object, it is not being used to match a pattern, but is instead being used as the replacement value. It gets its .toString() method called on it, giving you a string with the regex delimiters. Since you aren't using it to match a pattern, converting it to a regular expression is unnecessary.

2 Comments

My reading suggests that replace() can't take a variable, only a Regex. Also, I can't pass the URL string directly into replace(), because that URL/variable changes depending on what the user does.
Replace takes a pattern (simple string or regex) in the first parameter, and the second is the string you are replacing it with, so that needs to be (or will be coverted to) a string.

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.