3

I vaguely learned how regex work earlier on my last question and thought that I would be able to use it with other strings. Apparently this is not the case. Below are the contents of a div called mqcontainer.

<a style="text-decoration:none;" href="http://i.imgur.com/TeC4R.png"><img src="http://i.imgur.com/TeC4R.png" alt="Posted Image">[br]<small></small></a><small><a href="http://i.imgur.com/TeC4R.png" class="view_full">View Full Image</a></small>

My goal is to filter out this string so that it instead shows [url=http://i.imgur.com/TeC4R.png]Image[/url] when I click a button. This is what I have been trying:

$("#containerbtn").click(function(){
$("#mqcontainer").each(function(){
  $(this).html(
    $(this).html().replace(
      /<a style="text-decoration:none;" href="(.*?)"><img src=".*?" alt="Posted Image">\[br\]<small><\/small><\/a><small><a href=".*?" class="view_full">View Full Image<\/a><\/small>/g,
      '[url=$1]Image[/url]'
    )
  );
});
});

It is not working no matter what I try. Can anyone offer me some insight into the problem?

0

3 Answers 3

3

the [br] should be escaped in the regexp: \[br\]

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

4 Comments

... No luck, though I should have caught that. Thanks.
This works for me: '<a style="text-decoration:none;" href="http://i.imgur.com/TeC4R.png"><img src="http://i.imgur.com/TeC4R.png" alt="Posted Image">\[br\]<small></small></a><small><a href="http://i.imgur.com/TeC4R.png" class="view_full">View Full Image</a></small>'.replace(/<a style="text-decoration:none;" href="(.*?)"><img src=".*?" alt="Posted Image">\[br\]<small><\/small><\/a><small><a href=".*?" class="view_full">View Full Image<\/a><\/small>/, '[url=$1]Image[/url]') You can run it in the Firebug Console. It uses the escaped [br].
@Makaze: you want to be careful of introducing bugs while correcting other bugs... As noted in my answer your edit introduces an extra ] that wasn't in your pre-edit version. I've tested the string in my version (admittedly via .NET Regex) and it works and I believe is basically what Baldrick told you to use.
Chris: Typo, fixed. I have tried Baldric's. The current code in my question up to date and it is not working. @powertac: I do not understand where to put that string. Can you use give me the full context? My lack of knowledge grows ever more apparent. Sigh...
1

Wrong:

$("#mqcontainer").each(function(){
  $(this).html(
    ...
  )};
)};

The code above can not be correct. Since there is only one div with the ID mqcontainer.

Try this:

$("#mqcontainer").html(
    $("#mqcontainer").html().replace(/<a style="text-decoration:none;" href="(.*?)"><img src=".*?" alt="Posted Image">\[br\]<small><\/small><\/a><small><a href=".*?" class="view_full">View Full Image<\/a><\/small>/g, '[url=$1]Image[/url]')
);

1 Comment

I'd argue on semantics that it is correct. The code will function perfectly since each is perfectly able to work on a single object collection. I will agree that it is bad code thoughand possibly indicative of errors if multiple things with the same ID are being used. Also I think that was more of a comment since it doesn't actually answer his question or help at all (well, you have a fixed regex at a glance but you didn't mention this change at all).
0
<a style="text-decoration:none;" href="(.*?)"><img src=".*?" alt="Posted Image">\[br\]<small><\/small><\/a><small><a href=".*?" class="view_full">View Full Image<\/a><\/small>

The above should work.

I removed an extraneous ] that was after your image and before the escape [br].

Edit: And a note to add that looking at the revision history after seeing powtac's comments above this is in fact an error you introduced on editing in his suggestion.

Comments

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.