Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Apr 2003
    Posts
    27
    Thanks
    0
    Thanked 0 Times in 0 Posts

    regex help please

    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.
    Code:
    	var re = /([b])(.*?)([\/b])/gis;
    	var newStr = newBrief.replace(re, '<b>$2</b>') ;
    TIA

    Nick

  • #2
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    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.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #3
    New Coder
    Join Date
    Apr 2003
    Posts
    27
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I have tried a few things and this is where I have got to -
    Code:
    	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';
    			}
    		else
    			{
    			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.

    TIA
    Nick

  • #4
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    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?
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #5
    New Coder
    Join Date
    Apr 2003
    Posts
    27
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you. Have got it working, though not quite as desired - the joys of having to accommodate IE5M users.

    Thanks again

  • #6
    Senior Coder
    Join Date
    Jun 2002
    Location
    near Oswestry
    Posts
    4,508
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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:
    Code:
    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:
    Code:
    if(refToString && /[pattern]/i.test(refToString) ) ...
    Last edited by brothercake; 01-13-2004 at 01:42 PM.
    "Why bother with accessibility? ... Because deep down you know that the web is attractive to people who aren't exactly like you." - Joe Clark


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •