0

The wysiwyg editor I'm using uses this to replace the <strong> with a span style. I modified to add a code block.

v=v.replace(/<strong>(.*)<\/strong>/gi,'<span style="font-weight: bold;">$1</span>');

This works fine until I highlight code with

v=v.replace(/<pre>(.*)<\/pre>/gi,'<div id="precode"> $1 </div>');

I have been looking and can't fine any info on replace. Can someone explain

Can someone explain this part of the code? I'm sure it will help others as well

.replace(/<strong>(.*)<\/strong>/gi,'

Is there something in this causing the it to add the new style to each line

if(id=='style'){
                var sel=document.createElement('select'),
                styles=obj.styles||[['Style',''],['Paragraph','<p>'],['Header 1','<h1>'],['Header 2','<h2>'],['Header 3','<h3>'],['Header 4','<h4>'],['Header 5','<h5>'],['Header 6','<h6>'],['CodeBlock','<pre>']],
                sl=styles.length, x=0;
                sel.className='testyle'; sel.onchange=new Function(this.n+'.ddaction(this,"formatblock")');
                for(x;x<sl;x++){
                    var style=styles[x];
                    sel.options[x]=new Option(style[0],style[1])
                }
2
  • replace is a string method which replaces wanted of the string with another. This wanted replacement could be regular expression too. In your case it means that it will find all <strong> tags and replace them with <span> tags (its a regular expression) Commented Aug 6, 2011 at 16:30
  • 1
    also... read this Commented Aug 6, 2011 at 16:31

1 Answer 1

1

When you pass the string to find in /.../gi it looks for all the occurences of that string in the origincal string and replaces it by the string we pass as second parameter. It does not modify the original string so you have to assign its output to some variable in order to use it. I hope this makes sense.

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

1 Comment

If I wrap this < pre> blah blaeeh </ pre> everything works fine but if I wrap this < pre> blah <br> blaeeh </ pre> It wraps them both like div id="precode">blah < /div> div id="precode">blaeeh< /div>

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.