2

Assuming we have a comment textarea where the user can enter this code:

[quote="comment-1"]

How can I replace that code before the form submits with the actual html content from <div id="comment-1"> ?

3 Answers 3

5

You could try something like this:

http://jsfiddle.net/5sYFT/1/

var text = $('textarea').val();

text = text.replace(/\[quote="comment-(\d+)"\]/g, function(str,p1) { return $('#comment-' + p1).text(); });

$('textarea').val(text);

It should match agains any numbered quote in the format you gave.

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

Comments

0

You can use regular expressions:

text = text.replace(/\[quote="([a-z0-9-]+)"]/gi, 
    function(s, id) { return $('#' + id).text(); }
);

Comments

0

If I understand you correctly, you wish to replace something like '[quote="comment-1"]' with ''.

In JavaScript:

// Where textarea is the reference to the textarea, as returned by document.getElementById
var text = textarea.value;
text = text.replace(/\[quote\="(comment\-1)"\]/g, '<div id="$1">');

In jQuery:

// Where textarea is the reference to the textarea, as returned by $()
var text = textarea.val();
text = text.replace(/\[quote\="(comment\-1)"\]/, '<div id="$1">');

Hope this helps!

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.