View Full Version : regex help please

01-09-2004, 02:19 PM
I'm trying to write a regex to perform a bbcode replace for a live preview in an editor. I'm using the typical - string - formatting for the bbcode. I have tried the following as a basic example, but it did not work as I expected.

var re = /([b])(.*?)([\/b])/gis;
var newStr = newBrief.replace(re, '<b>$2</b>') ;



01-09-2004, 03:17 PM
1. Have a look at those flags. 's' is not a regex flag in JavaScript.
2. Remember that brackets [] are special characters. You need to escape them.

You could always have a look at my regex tutorial. Links are in my sig.

01-12-2004, 06:11 PM
I have tried a few things and this is where I have got to -

function toggleEditorPreview()
if (document.getElementById('previewPane').style.display == 'none')
var previewPane = document.getElementById('previewPane');
var newBrief = document.getElementById('largeBriefs').value;

var re = /(\[b\])(.*?)(\[\/b\])/gi;
var newStr = newBrief.replace(re, '<b>$2</b>') ;

re = /(\[i\])(.*?)(\[\/i\])/gi;
newStr = newStr.replace(re, '<i>$2</i>') ;

re = /(\[u\])(.*?)(\[\/u\])/gi;
newStr = newStr.replace(re, '<u>$2</u>') ;

re = /(\[t\])/gi;
newStr = newStr.replace(re, '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;') ;

re = /(\[pb\])/gi;
newStr = newStr.replace(re, '<hr style="page-break-before:always;" />') ;

re = new RegExp("(\n)", "gi");
newStr = newStr.replace(re, '<br />') ;

previewPane.innerHTML = newStr;
document.getElementById('editorPane').style.display = 'none';
document.getElementById('previewPane').style.display = 'block';
document.getElementById('previewPane').style.display = 'none';
document.getElementById('editorPane').style.display = 'block';

This works fine in all of our test browsers, except for IE5 on Mac OS9.2 and IE5.2 on Mac OSX. I have searched around the net trying to find a solution, but not got very far.

Any suggestions would be very much appreciated.


01-12-2004, 06:44 PM
Non-greedy quantifiers were introduced with the JScript 5.5 engine on windows - ie5.5w, that is. The ie5m JScript engine is the same as the engine in ie5.0w. May that be the problem source?

01-13-2004, 11:52 AM
Thank you. Have got it working, though not quite as desired - the joys of having to accommodate IE5M users.

Thanks again

01-13-2004, 12:34 PM
Incidentally (might be relevant) mac/ie5 has some fairly serious issues with regular expression handling:

- IE5.0 (for OS9 or earlier) doesn't properly garbage collect the RegExp constructor, so if you're iterating more than a few times you may get an "out of memory" error; you can avoid that by using a regex literal:

if( /[pattern]/i.test(refToString) ) ...

- MSN for OSX has either a memory leak, or possibly over-zealous error handling - if you try to test() an empty string it abandons the script with another "out of memory" error; so for safety:

if(refToString && /[pattern]/i.test(refToString) ) ...